﻿using DevComponents.DotNetBar;
using DevComponents.DotNetBar.SuperGrid;
using DevComponents.DotNetBar.SuperGrid.Style;
using Kimd.Common;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;
using WorkStation.Logic;
using WorkStation.UI.Controls;

namespace WorkStation.UI
{
    public partial class FormXYZR : Office2007Form
    {
        private string _moduleName;
        private FormMain _mainForm;
        private MotionBoard xy_Displayer;
        private DDControl dd_Displayer;
        public string XYNames { get; set; }
        public List<string> HNames { get; set; }
        public List<string> ZNames { get; set; }
        public List<string> RNames { get; set; }
        public string DDName { get; set; }
        public List<Sensor> Sensors { get; set; }
        public List<Cylinder> Cylinders { get; set; }
        public List<int> ContainerSplitDistance { get; set; }
        public FormXYZR(string moduleName, FormMain mainForm)
        {
            InitializeComponent();
            this.EnableGlass = false;
            this._moduleName = moduleName;
            this._mainForm = mainForm;
            ContainerSplitDistance = new List<int>();
        }
        private void FormXYZR_Load(object sender, EventArgs e)
        {
            resizePanel();

            if (!string.IsNullOrEmpty(XYNames))
            {
                xy_Displayer = new MotionBoard(XYNames);
                xy_Displayer._marginLeft = 60;
                xy_Displayer._marginRight = 50;
                xy_Displayer._marginUp = 50;
                xy_Displayer._marginDown = 50;
                xy_Displayer.Dock = DockStyle.Fill;
                xy_Displayer.CreateTable();
                xy_Displayer.OnGoToBoardPoint += GoToBoardPoint;
                xy_Displayer.OnKeyboardEnableClick += KeyboardEnabled;
                this.panelXY.Controls.Add(xy_Displayer);

                xy_Displayer.CreateCoordinate("X", AxisDir.Right2Left, AxisLocation.Down, -200, 1200);
                string xName = XYNames.Split(',')[0];
                if (SingletonProvider<MachineResource>.Instance.MachineAxis[xName].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[xName].AxisLimits.Count > 0)
                {
                    double posLimit = 0;
                    double negLimit = 0;
                    foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[xName].AxisLimits)
                    {
                        if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(xName))
                        {
                            posLimit = item.Position;
                        }
                        if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(xName))
                        {
                            negLimit = item.Position;
                        }
                    }
                    if (posLimit != 0 || negLimit != 0)
                        this.xy_Displayer.SetXLimit(posLimit + 100, negLimit - 100);
                }
                if (SingletonProvider<MachineResource>.Instance.MachineAxis[xName].ShowLocation == 0)
                {
                    xy_Displayer.CoordinateX.Location = AxisLocation.Up;
                    xy_Displayer.CoordinateX.Direction = AxisDir.Left2Right;
                }
                else if (SingletonProvider<MachineResource>.Instance.MachineAxis[xName].ShowLocation == 1)
                {
                    xy_Displayer.CoordinateX.Location = AxisLocation.Down;
                    xy_Displayer.CoordinateX.Direction = AxisDir.Left2Right;
                }
                else if (SingletonProvider<MachineResource>.Instance.MachineAxis[xName].ShowLocation == 2)
                {
                    xy_Displayer.CoordinateX.Location = AxisLocation.Up;
                    xy_Displayer.CoordinateX.Direction = AxisDir.Right2Left;
                }
                else if (SingletonProvider<MachineResource>.Instance.MachineAxis[xName].ShowLocation == 3)
                {
                    xy_Displayer.CoordinateX.Location = AxisLocation.Down;
                    xy_Displayer.CoordinateX.Direction = AxisDir.Right2Left;
                }
                xy_Displayer.CreateCoordinate("Y", AxisDir.Down2Up, AxisLocation.Right, -50, 1500);
                string yName = XYNames.Split(',')[1];
                if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].AxisLimits != null &&
                    SingletonProvider<MachineResource>.Instance.MachineAxis[yName].AxisLimits.Count > 0)
                {
                    double posLimit = 0;
                    double negLimit = 0;
                    foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[yName].AxisLimits)
                    {
                        if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(yName))
                        {
                            posLimit = item.Position;
                        }
                        if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(yName))
                        {
                            negLimit = item.Position;
                        }
                    }
                    if (posLimit != 0 || negLimit != 0)
                        this.xy_Displayer.SetYLimit(posLimit + 100, negLimit - 100);
                }
                if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation != 0)
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 4)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Left;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Up2Down;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 5)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Left;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Down2Up;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 6)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Right;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Up2Down;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 7)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Right;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Down2Up;
                    }
                }

                addLimitItem(XYNames.Split(',')[0]);
                addLimitItem(XYNames.Split(',')[1]);

                //addLayoutItem(XYNames.Split(',')[0], AxisType.H, false);
                //addLayoutItem(XYNames.Split(',')[1], AxisType.V, false);

                //foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].PositionLimits)
                //{
                //    xy_Displayer.CreateLimitArea("abc", item.SectionStartPosition, item.RelatedStartPosition, item.SectionEndPosition, item.RelatedEndPosition);
                //}

                xy_Displayer.CreateDirectionRect("XY");
                xy_Displayer.NotifyDirectRectMouseDown -= DirectRectMouseDown;
                xy_Displayer.NotifyDirectRectMouseDown += DirectRectMouseDown;
                xy_Displayer.NotifyDirectRectMouseUp -= DirectRectMouseUp;
                xy_Displayer.NotifyDirectRectMouseUp += DirectRectMouseUp;
                xy_Displayer.NotifyStopClick -= StopClicked;
                xy_Displayer.NotifyStopClick += StopClicked;
                xy_Displayer.UpdateBoard();
            }

            if (ZNames != null && ZNames.Count > 0)
            {
                int index = 0;
                foreach (var zname in ZNames)
                {
                    AddZTab(zname);
                    MotionBoard z_Displayer = new MotionBoard(zname);
                    z_Displayer.OnGoToBoardPoint += GoToBoardPoint;
                    z_Displayer.OnKeyboardEnableClick += KeyboardEnabled;
                    z_Displayer.Dock = DockStyle.Fill;
                    z_Displayer.CreatedShapes = new List<MotionShape>();
                    z_Displayer.CreateCoordinate("Z", AxisDir.Up2Down, AxisLocation.Left, -100, 300);
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[zname].ShowLocation == 0)
                        SingletonProvider<MachineResource>.Instance.MachineAxis[zname].ShowLocation = 4;
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[zname].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[zname].AxisLimits.Count > 0)
                    {
                        double posLimit = 0;
                        double negLimit = 0;
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[zname].AxisLimits)
                        {
                            if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(zname))
                            {
                                posLimit = item.Position;
                            }
                            if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(zname))
                            {
                                negLimit = item.Position;
                            }
                        }
                        if (posLimit != 0 || negLimit != 0)
                            z_Displayer.SetYLimit(posLimit + 100, negLimit - 100);
                    }
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[zname].ShowLocation != 0)
                    {
                        if (SingletonProvider<MachineResource>.Instance.MachineAxis[zname].ShowLocation == 4)
                        {
                            z_Displayer.CoordinateY.Location = AxisLocation.Left;
                            z_Displayer.CoordinateY.Direction = AxisDir.Up2Down;
                        }
                        else if (SingletonProvider<MachineResource>.Instance.MachineAxis[zname].ShowLocation == 5)
                        {
                            z_Displayer.CoordinateY.Location = AxisLocation.Left;
                            z_Displayer.CoordinateY.Direction = AxisDir.Down2Up;
                        }
                    }
                    z_Displayer.CreateTable();
                    z_Displayer._marginUp = 10;
                    z_Displayer._marginLeft = 130;
                    z_Displayer._marginRight = 60;
                    z_Displayer._marginDown = 10;
                    //z_Displayer.NotifyCreatePointRequest -= onCreatePointRequest;
                    //z_Displayer.NotifyCreatePointRequest += onCreatePointRequest;
                    (sTC_Z.Tabs[index] as SuperTabItem).AttachedControl.Controls.Add(z_Displayer);

                    addLimitItem(zname);
                    //addLayoutItem(zname, AxisType.V, true);

                    z_Displayer.CreateDirectionRect("Z");
                    z_Displayer.NotifyDirectRectMouseDown -= DirectRectMouseDown;
                    z_Displayer.NotifyDirectRectMouseDown += DirectRectMouseDown;
                    z_Displayer.NotifyDirectRectMouseUp -= DirectRectMouseUp;
                    z_Displayer.NotifyDirectRectMouseUp += DirectRectMouseUp;
                    z_Displayer.NotifyStopClick -= StopClicked;
                    z_Displayer.NotifyStopClick += StopClicked;
                    z_Displayer.UpdateBoard();

                    index++;
                }
            }

            if (RNames != null && RNames.Count > 0)
            {
                int index = 0;
                foreach (var rname in RNames)
                {
                    AddRTab(rname);
                    MotionBoard r_Displayer = new MotionBoard(rname);
                    r_Displayer.OnGoToBoardPoint += GoToBoardPoint;
                    r_Displayer.OnKeyboardEnableClick += KeyboardEnabled;
                    r_Displayer.Dock = DockStyle.Fill;
                    r_Displayer.CreatedShapes = new List<MotionShape>();
                    r_Displayer._marginLeft = r_Displayer._marginRight = 20;
                    r_Displayer._marginUp = r_Displayer._marginDown = 20;
                    r_Displayer.CreateCoordinate("R", AxisDir.Circle, AxisLocation.Left, -40, 180);
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].ShowLocation != 0)
                    {
                        if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].ShowLocation == 8)
                        {
                            r_Displayer.CoordinateR.IsCCW = false;
                        }
                        else if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].ShowLocation == 9)
                        {
                            r_Displayer.CoordinateR.IsCCW = true;
                        }
                    }
                    r_Displayer.CreateTable();
                    //r_Displayer.NotifyCreatePointRequest -= onCreatePointRequest;
                    //r_Displayer.NotifyCreatePointRequest += onCreatePointRequest;
                    (sTC_R.Tabs[index] as SuperTabItem).AttachedControl.Controls.Add(r_Displayer);
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[rname].AxisLimits.Count > 0)
                    {
                        double posLimit = 0;
                        double negLimit = 0;
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[rname].AxisLimits)
                        {
                            if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(rname))
                            {
                                posLimit = item.Position;
                            }
                            if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(rname))
                            {
                                negLimit = item.Position;
                            }
                        }
                        if (posLimit != 0 || negLimit != 0)
                            r_Displayer.SetRLimit(posLimit, negLimit);
                    }
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].ShowLocation != 0)
                    {
                        if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].ShowLocation == 8)
                        {
                            r_Displayer.CoordinateR.SetPieClock(false);
                        }
                        else if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].ShowLocation == 9)
                        {
                            r_Displayer.CoordinateR.SetPieClock(true);
                        }
                    }


                    addLimitItem(rname);
                    addLayoutItem(rname, AxisType.R, true);

                    r_Displayer.CreateDirectionRect("R");
                    r_Displayer.NotifyDirectRectMouseDown -= DirectRectMouseDown;
                    r_Displayer.NotifyDirectRectMouseDown += DirectRectMouseDown;
                    r_Displayer.NotifyDirectRectMouseUp -= DirectRectMouseUp;
                    r_Displayer.NotifyDirectRectMouseUp += DirectRectMouseUp;
                    r_Displayer.NotifyStopClick -= StopClicked;
                    r_Displayer.NotifyStopClick += StopClicked;
                    r_Displayer.UpdateBoard();
                    index++;
                }
            }

            if (HNames != null && HNames.Count > 0)
            {
                int index = 0;
                foreach (var hname in HNames)
                {
                    AddHTab(hname);
                    MotionBoard h_Displayer = new MotionBoard(hname);
                    h_Displayer.OnGoToBoardPoint += GoToBoardPoint;
                    h_Displayer.OnKeyboardEnableClick += KeyboardEnabled;
                    h_Displayer.Dock = DockStyle.Fill;
                    h_Displayer.CreatedShapes = new List<MotionShape>();
                    h_Displayer.CreateCoordinate("X", AxisDir.Left2Right, AxisLocation.Down, -100, 600);
                    if (!SingletonProvider<MachineResource>.Instance.MachineAxis.ContainsKey(hname))
                        return;
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[hname].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[hname].AxisLimits.Count > 0)
                    {
                        double posLimit = 0;
                        double negLimit = 0;
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[hname].AxisLimits)
                        {
                            if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(hname))
                            {
                                posLimit = item.Position;
                            }
                            if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(hname))
                            {
                                negLimit = item.Position;
                            }
                        }
                        if (posLimit != 0 || negLimit != 0)
                            h_Displayer.SetXLimit(posLimit + 100, negLimit - 100);
                    }
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[hname].ShowLocation == 0)
                    {
                        h_Displayer.CoordinateX.Location = AxisLocation.Up;
                        h_Displayer.CoordinateX.Direction = AxisDir.Left2Right;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[hname].ShowLocation == 1)
                    {
                        h_Displayer.CoordinateX.Location = AxisLocation.Down;
                        h_Displayer.CoordinateX.Direction = AxisDir.Left2Right;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[hname].ShowLocation == 2)
                    {
                        h_Displayer.CoordinateX.Location = AxisLocation.Up;
                        h_Displayer.CoordinateX.Direction = AxisDir.Right2Left;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[hname].ShowLocation == 3)
                    {
                        h_Displayer.CoordinateX.Location = AxisLocation.Down;
                        h_Displayer.CoordinateX.Direction = AxisDir.Right2Left;
                    }
                    h_Displayer.CreateTable();
                    //h_Displayer._marginUp = 10;
                    //h_Displayer._marginLeft = 130;
                    //h_Displayer._marginRight = 60;
                    //h_Displayer._marginDown = 10;
                    (sTC_H.Tabs[index] as SuperTabItem).AttachedControl.Controls.Add(h_Displayer);

                    addLimitItem(hname);
                    //addLayoutItem(hname, AxisType.H, true);

                    h_Displayer.CreateDirectionRect("H");
                    h_Displayer.NotifyDirectRectMouseDown -= DirectRectMouseDown;
                    h_Displayer.NotifyDirectRectMouseDown += DirectRectMouseDown;
                    h_Displayer.NotifyDirectRectMouseUp -= DirectRectMouseUp;
                    h_Displayer.NotifyDirectRectMouseUp += DirectRectMouseUp;
                    h_Displayer.NotifyStopClick -= StopClicked;
                    h_Displayer.NotifyStopClick += StopClicked;
                    h_Displayer.UpdateBoard();

                    index++;
                }
            }

            if (!string.IsNullOrEmpty(DDName))
            {
                dd_Displayer = new DDControl();
                List<Cylinder> loadLiftCylinder1 = new List<Cylinder>();
                loadLiftCylinder1.Add(MachineResource.Instance.MachineCylinder["ST1顶升气缸V"]);
                loadLiftCylinder1.Add(null);
                loadLiftCylinder1.Add(null);
                loadLiftCylinder1.Add(MachineResource.Instance.MachineCylinder["ST4上料顶升气缸V(已测)"]);
                loadLiftCylinder1.Add(null);
                loadLiftCylinder1.Add(MachineResource.Instance.MachineCylinder["ST6顶升气缸V"]);

                List<Cylinder> unloadLiftCylinder2 = new List<Cylinder>();
                unloadLiftCylinder2.Add(null);
                unloadLiftCylinder2.Add(null);
                unloadLiftCylinder2.Add(null);
                unloadLiftCylinder2.Add(MachineResource.Instance.MachineCylinder["ST4下料顶升气缸V(待测)"]);
                unloadLiftCylinder2.Add(null);
                unloadLiftCylinder2.Add(null);
                DDMotor dd = new DDMotor(MachineResource.Instance.MachinePdo["DD控制地址"],
                                         MachineResource.Instance.MachinePdo["DD位置地址1"],
                                         MachineResource.Instance.MachinePdo["DD位置地址2"],
                                         loadLiftCylinder1,
                                         unloadLiftCylinder2);
                dd_Displayer.SetMotor(dd);
                dd_Displayer.Dock = DockStyle.Fill;
                this.panelXY.Controls.Add(dd_Displayer);
            }
            createPointGrid();
            initDIOGrid();
            if (Sensors != null && Sensors.Count > 0)
                Sensors.ForEach(s => addInputColumn(s.Description, s));
            if (Cylinders != null && Cylinders.Count > 0)
                Cylinders.ForEach(c => addOutputColumn(c.Description, c));

            loadPoint();
            loadTray();
            t_IO.Enabled = true;

            tSB_Mode.Checked = true;
            if (tSB_Mode.Checked)
            {
                tSB_Mode.Text = "寸动";
                tSB_Mode.Image = Properties.Resources.寸动;
                ContiMove = false;
                //tSB_Mode.BackColor = Color.YellowGreen;
                tSDDB_Step.Enabled = true;
                tSL_Step.Enabled = true;
            }
            else
            {
                tSB_Mode.Text = "连续";
                tSB_Mode.Image = Properties.Resources.连续;
                ContiMove = true;
                //tSB_Mode.BackColor = Color.BlueViolet;
                tSDDB_Step.Enabled = false;
                tSL_Step.Enabled = false;
            }
        }
        private async void DirectRectMouseDown(string name, RectDirection direction)
        {

#if DEBUGWITHOUTMACHINE
#else
            string stopReason = "";
            if (name == XYNames)
            {
                string xName = XYNames.Split(',')[0];
                string yName = XYNames.Split(',')[1];
                bool dir = false;
                double curPos = 0;
                switch (direction)
                {
                    case RectDirection.Left:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[xName].ShowLocation < 2 ? false : true;
                        break;
                    case RectDirection.Right:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[xName].ShowLocation < 2 ? true : false;
                        break;
                    case RectDirection.Up:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 4 || SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 6 ? false : true;
                        break;
                    case RectDirection.Down:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 4 || SingletonProvider<MachineResource>.Instance.MachineAxis[yName].ShowLocation == 6 ? true : false;
                        break;
                }
                switch (direction)
                {
                    case RectDirection.Left:
                    case RectDirection.Right:
                        SingletonProvider<MachineResource>.Instance.MachineAxis[xName].SetSpeed(SpeedType.Manual);
                        if (ContiMove)
                        {
                            SingletonProvider<MachineResource>.Instance.MachineAxis[xName].MoveContinuous(dir, out stopReason);
                        }
                        else
                        {
                            await Task.Run(() =>
                            {
                                curPos = SingletonProvider<MachineResource>.Instance.MachineAxis[xName].Position;
                                if (dir)
                                    SingletonProvider<MachineResource>.Instance.MachineAxis[xName].MoveAbsolute(curPos + MoveStep, out stopReason, 0);
                                else
                                    SingletonProvider<MachineResource>.Instance.MachineAxis[xName].MoveAbsolute(curPos - MoveStep, out stopReason, 0);
                            });
                        }
                        break;
                    case RectDirection.Up:
                    case RectDirection.Down:
                        SingletonProvider<MachineResource>.Instance.MachineAxis[yName].SetSpeed(SpeedType.Manual);
                        if (ContiMove)
                        {
                            SingletonProvider<MachineResource>.Instance.MachineAxis[yName].MoveContinuous(dir, out stopReason);
                        }
                        else
                        {
                            await Task.Run(() =>
                            {
                                curPos = SingletonProvider<MachineResource>.Instance.MachineAxis[yName].Position;
                                if (dir)
                                    SingletonProvider<MachineResource>.Instance.MachineAxis[yName].MoveAbsolute(curPos + MoveStep, out stopReason, 0);
                                else
                                    SingletonProvider<MachineResource>.Instance.MachineAxis[yName].MoveAbsolute(curPos - MoveStep, out stopReason, 0);
                            });
                        }
                        break;
                }
            }
            else if (ZNames != null && ZNames.Contains(name))
            {
                bool dir = false;
                double curPos = 0;
                switch (direction)
                {
                    case RectDirection.Up:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 4 || SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 6 ? false : true;
                        break;
                    case RectDirection.Down:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 5 || SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 7 ? false : true;
                        break;
                }
                SingletonProvider<MachineResource>.Instance.MachineAxis[name].SetSpeed(SpeedType.Manual);
                if (ContiMove)
                    SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveContinuous(dir, out stopReason);
                else
                {
                    await Task.Run(() =>
                    {
                        curPos = SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position;
                        if (dir)
                            SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveAbsolute(curPos + MoveStep, out stopReason, 0);
                        else
                            SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveAbsolute(curPos - MoveStep, out stopReason, 0);
                    });
                }
            }
            else if (HNames != null && HNames.Contains(name))
            {
                bool dir = false;
                double curPos = 0;
                switch (direction)
                {
                    case RectDirection.Left:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 0 || SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 2 ? false : true;
                        break;
                    case RectDirection.Right:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 1 || SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 3 ? false : true;
                        break;
                }
                SingletonProvider<MachineResource>.Instance.MachineAxis[name].SetSpeed(SpeedType.Manual);
                if (ContiMove)
                    SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveContinuous(dir, out stopReason);
                else
                {
                    await Task.Run(() =>
                    {
                        curPos = SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position;
                        if (dir)
                            SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveAbsolute(curPos + MoveStep, out stopReason, 0);
                        else
                            SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveAbsolute(curPos - MoveStep, out stopReason, 0);
                    });
                }
            }
            else if (RNames != null && RNames.Contains(name))
            {
                bool dir = false;
                double curPos = 0;
                switch (direction)
                {
                    case RectDirection.Left:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 8 ? true : false;
                        break;
                    case RectDirection.Right:
                        dir = SingletonProvider<MachineResource>.Instance.MachineAxis[name].ShowLocation == 9 ? true : false;
                        break;
                }
                SingletonProvider<MachineResource>.Instance.MachineAxis[name].SetSpeed(SpeedType.Manual);
                if (ContiMove)
                    SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveContinuous(dir, out stopReason);
                else
                {
                    await Task.Run(() =>
                    {
                        curPos = SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position;
                        if (dir)
                            SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveAbsolute(curPos + MoveStep, out stopReason, 0);
                        else
                            SingletonProvider<MachineResource>.Instance.MachineAxis[name].MoveAbsolute(curPos - MoveStep, out stopReason, 0);
                    });
                }
            }
            if (!string.IsNullOrEmpty(stopReason))
            {
                this.BeginInvoke(new Action(() =>
                {
                    SystemInfo.ShowToastInfo(this, stopReason, true);
                }));
            }
#endif
        }
        private void DirectRectMouseUp(string name)
        {
#if DEBUGWITHOUTMACHINE
#else
            if (name == XYNames)
            {
                if (ContiMove)
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].Stop(false);
                    SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].Stop(false);
                }
            }
            else if (ZNames != null && ZNames.Contains(name))
            {
                if (ContiMove)
                {
                    foreach (var item in ZNames)
                    {
                        SingletonProvider<MachineResource>.Instance.MachineAxis[item].Stop(false);
                    }
                }
            }
            else if (RNames != null && RNames.Contains(name))
            {
                if (ContiMove)
                {
                    foreach (var item in RNames)
                    {
                        SingletonProvider<MachineResource>.Instance.MachineAxis[item].Stop(false);
                    }
                }
            }
            else if (HNames != null && HNames.Contains(name))
            {
                if (ContiMove)
                {
                    foreach (var item in HNames)
                    {
                        SingletonProvider<MachineResource>.Instance.MachineAxis[item].Stop(false);
                    }
                }
            }
#endif
        }
        private void StopClicked(string name)
        {
#if DEBUGWITHOUTMACHINE
#else
            if (name == XYNames)
            {
                SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].Stop(false);
                SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].Stop(false);
            }
            else if (ZNames.Contains(name))
            {
                foreach (var item in ZNames)
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[item].Stop(false);
                }
            }
            else if (RNames.Contains(name))
            {
                foreach (var item in RNames)
                {
                    SingletonProvider<MachineResource>.Instance.MachineAxis[item].Stop(false);
                }
            }
#endif
        }
        private void addLimitItem(string axisName)
        {
            ToolStripMenuItem tsmi = new System.Windows.Forms.ToolStripMenuItem();
            tsmi.Size = new System.Drawing.Size(180, 30);
            tsmi.Text = axisName;
            tsmi.Click += Tsmi_Click;

            tSDDB_Limit.DropDownItems.Add(tsmi);
        }
        private void addLayoutItem(string axisName, AxisType axisType, bool isSingle)
        {
            ToolStripMenuItem tsmi = new System.Windows.Forms.ToolStripMenuItem();
            tsmi.Size = new System.Drawing.Size(180, 30);
            tsmi.Text = axisName;

            for (int i = 0; i < 8; i++)
            {
                ToolStripMenuItem tsmiSub = new ToolStripMenuItem();
                if (i == 0 && axisType == AxisType.H)
                {
                    tsmiSub.Text = "上-左到右";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 0;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 1 && axisType == AxisType.H && !isSingle)
                {
                    tsmiSub.Text = "下-左到右";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 1;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 2 && axisType == AxisType.H)
                {
                    tsmiSub.Text = "上-右到左";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 2;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 3 && axisType == AxisType.H && !isSingle)
                {
                    tsmiSub.Text = "下-右到左";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 3;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 4 && axisType == AxisType.V)
                {
                    tsmiSub.Text = "左-上到下";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 4;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 5 && axisType == AxisType.V)
                {
                    tsmiSub.Text = "左-下到上";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 5;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 6 && axisType == AxisType.V && !isSingle)
                {
                    tsmiSub.Text = "右-上到下";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 6;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
                else if (i == 7 && axisType == AxisType.V && !isSingle)
                {
                    tsmiSub.Text = "右-下到上";
                    tsmiSub.Name = $"{axisName}-{i}";
                    tsmiSub.Click += TsmiSub_Click;
                    tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 7;
                    tsmi.DropDownItems.Add(tsmiSub);
                }
            }
            if (axisType == AxisType.R)
            {
                ToolStripMenuItem tsmiSub = new ToolStripMenuItem();
                tsmiSub.Text = "顺时针";
                tsmiSub.Name = $"{axisName}-{8}";
                tsmiSub.Click += TsmiSub_Click;
                tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 8;
                tsmi.DropDownItems.Add(tsmiSub);

                tsmiSub = new ToolStripMenuItem();
                tsmiSub.Text = "逆时针";
                tsmiSub.Name = $"{axisName}-{9}";
                tsmiSub.Click += TsmiSub_Click;
                tsmiSub.Checked = SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 9;
                tsmi.DropDownItems.Add(tsmiSub);
            }

            tSDDB_Show.DropDownItems.Add(tsmi);
        }

        private void TsmiSub_Click(object sender, EventArgs e)
        {
            string axisName = (sender as ToolStripMenuItem).Name.Split('-')[0];
            SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation = Convert.ToInt32((sender as ToolStripMenuItem).Name.Split('-')[1]);
            if (XYNames != null && XYNames.Contains(axisName))
            {
                if (XYNames.Split(',')[0].Equals(axisName))
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 0)
                    {
                        xy_Displayer.CoordinateX.Location = AxisLocation.Up;
                        xy_Displayer.CoordinateX.Direction = AxisDir.Left2Right;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 1)
                    {
                        xy_Displayer.CoordinateX.Location = AxisLocation.Down;
                        xy_Displayer.CoordinateX.Direction = AxisDir.Left2Right;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 2)
                    {
                        xy_Displayer.CoordinateX.Location = AxisLocation.Up;
                        xy_Displayer.CoordinateX.Direction = AxisDir.Right2Left;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 3)
                    {
                        xy_Displayer.CoordinateX.Location = AxisLocation.Down;
                        xy_Displayer.CoordinateX.Direction = AxisDir.Right2Left;
                    }
                }
                if (XYNames.Split(',')[1].Equals(axisName))
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 4)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Left;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Up2Down;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 5)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Left;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Down2Up;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 6)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Right;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Up2Down;
                    }
                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 7)
                    {
                        xy_Displayer.CoordinateY.Location = AxisLocation.Right;
                        xy_Displayer.CoordinateY.Direction = AxisDir.Down2Up;
                    }
                }
                xy_Displayer.UpdateBoard();
                MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                if (ZNames.Contains(axisName))
                {
                    foreach (SuperTabItem item in sTC_Z.Tabs)
                    {
                        if (item.Name.Equals(axisName))
                        {
                            foreach (Control control in item.AttachedControl.Controls)
                            {
                                if (control is MotionBoard)
                                {
                                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 4)
                                    {
                                        (control as MotionBoard).CoordinateY.Location = AxisLocation.Left;
                                        (control as MotionBoard).CoordinateY.Direction = AxisDir.Up2Down;
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 5)
                                    {
                                        (control as MotionBoard).CoordinateY.Location = AxisLocation.Left;
                                        (control as MotionBoard).CoordinateY.Direction = AxisDir.Down2Up;
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 6)
                                    {
                                        (control as MotionBoard).CoordinateY.Location = AxisLocation.Right;
                                        (control as MotionBoard).CoordinateY.Direction = AxisDir.Up2Down;
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 7)
                                    {
                                        (control as MotionBoard).CoordinateY.Location = AxisLocation.Right;
                                        (control as MotionBoard).CoordinateY.Direction = AxisDir.Down2Up;
                                    }
                                    (control as MotionBoard).UpdateBoard();
                                    control.Refresh();
                                }
                            }
                            MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                            break;
                        }
                    }
                }
            }
            if (RNames != null && RNames.Count > 0)
            {
                if (RNames.Contains(axisName))
                {
                    foreach (SuperTabItem item in sTC_R.Tabs)
                    {
                        if (item.Name.Equals(axisName))
                        {
                            foreach (Control control in item.AttachedControl.Controls)
                            {
                                if (control is MotionBoard)
                                {
                                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 8)
                                    {
                                        (control as MotionBoard).CoordinateR.IsCCW = false;
                                        (control as MotionBoard).CoordinateR.SetPieClock(false);
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 9)
                                    {
                                        (control as MotionBoard).CoordinateR.IsCCW = true;
                                        (control as MotionBoard).CoordinateR.SetPieClock(true);
                                    }
                                    (control as MotionBoard).UpdateBoard();
                                    control.Refresh();
                                }
                            }
                            MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                            break;
                        }
                    }
                }
            }
            if (HNames != null && HNames.Count > 0)
            {
                if (HNames.Contains(axisName))
                {
                    foreach (SuperTabItem item in sTC_H.Tabs)
                    {
                        if (item.Name.Equals(axisName))
                        {
                            foreach (Control control in item.AttachedControl.Controls)
                            {
                                if (control is MotionBoard)
                                {
                                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 0)
                                    {
                                        (control as MotionBoard).CoordinateX.Location = AxisLocation.Up;
                                        (control as MotionBoard).CoordinateX.Direction = AxisDir.Left2Right;
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 1)
                                    {
                                        (control as MotionBoard).CoordinateX.Location = AxisLocation.Down;
                                        (control as MotionBoard).CoordinateX.Direction = AxisDir.Left2Right;
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 2)
                                    {
                                        (control as MotionBoard).CoordinateX.Location = AxisLocation.Up;
                                        (control as MotionBoard).CoordinateX.Direction = AxisDir.Right2Left;
                                    }
                                    else if (SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].ShowLocation == 3)
                                    {
                                        (control as MotionBoard).CoordinateX.Location = AxisLocation.Down;
                                        (control as MotionBoard).CoordinateX.Direction = AxisDir.Right2Left;
                                    }
                                    (control as MotionBoard).UpdateBoard();
                                    control.Refresh();
                                }
                            }
                            MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                            break;
                        }
                    }
                }
            }
        }

        private void Tsmi_Click(object sender, EventArgs e)
        {
            string axisName = (sender as ToolStripMenuItem).Text;
            FormLimitSetting limitForm = new FormLimitSetting(axisName);
            limitForm.motionLimitControl1.ModuleName = MachineResource.Instance.MachineAxis[axisName].Module;
            limitForm.ShowDialog();
            if (!string.IsNullOrEmpty(XYNames))
            {
                string xName = XYNames.Split(',')[0];
                if (SingletonProvider<MachineResource>.Instance.MachineAxis[xName].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[xName].AxisLimits.Count > 0)
                {
                    double posLimit = 0;
                    double negLimit = 0;
                    foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[xName].AxisLimits)
                    {
                        if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(xName))
                        {
                            posLimit = item.Position;
                        }
                        if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(xName))
                        {
                            negLimit = item.Position;
                        }
                    }
                    if (posLimit != 0 || negLimit != 0)
                        this.xy_Displayer.SetXLimit(posLimit + 100, negLimit - 100);
                }
                string yName = XYNames.Split(',')[1];
                if (SingletonProvider<MachineResource>.Instance.MachineAxis[yName].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[yName].AxisLimits.Count > 0)
                {
                    double posLimit = 0;
                    double negLimit = 0;
                    foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[yName].AxisLimits)
                    {
                        if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(yName))
                        {
                            posLimit = item.Position;
                        }
                        if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(yName))
                        {
                            negLimit = item.Position;
                        }
                    }
                    if (posLimit != 0 || negLimit != 0)
                        this.xy_Displayer.SetYLimit(posLimit + 100, negLimit - 100);
                }
                xy_Displayer.UpdateBoard();
                panelXY.Refresh();
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                foreach (var zname in ZNames)
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[zname].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[zname].AxisLimits.Count > 0)
                    {
                        double posLimit = 0;
                        double negLimit = 0;
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[zname].AxisLimits)
                        {
                            if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(zname))
                            {
                                posLimit = item.Position;
                            }
                            if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(zname))
                            {
                                negLimit = item.Position;
                            }
                        }
                        if (posLimit != 0 || negLimit != 0)
                        {
                            foreach (Control c in sTC_Z.SelectedTab.AttachedControl.Controls)
                            {
                                (c as MotionBoard).SetYLimit(posLimit + 100, negLimit - 100);
                                (c as MotionBoard).UpdateBoard();
                                c.Refresh();
                            }
                        }
                    }
                }
            }

            if (RNames != null && RNames.Count > 0)
            {
                foreach (var rname in RNames)
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[rname].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[rname].AxisLimits.Count > 0)
                    {
                        double posLimit = 0;
                        double negLimit = 0;
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[rname].AxisLimits)
                        {
                            if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(rname))
                            {
                                posLimit = item.Position;
                            }
                            if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(rname))
                            {
                                negLimit = item.Position;
                            }
                        }
                        if (posLimit != 0 && negLimit != 0)
                        {
                            foreach (Control c in sTC_R.SelectedTab.AttachedControl.Controls)
                            {
                                (c as MotionBoard).SetRLimit(posLimit, negLimit);
                                (c as MotionBoard).UpdateBoard();
                                c.Refresh();
                            }
                        }
                    }
                }
            }

            if (HNames != null && HNames.Count > 0)
            {
                foreach (var hname in HNames)
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[hname].AxisLimits != null && SingletonProvider<MachineResource>.Instance.MachineAxis[hname].AxisLimits.Count > 0)
                    {
                        double posLimit = 0;
                        double negLimit = 0;
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis[hname].AxisLimits)
                        {
                            if (item.LimitType == LimitType.SoftPositiveLimit && item.LimitAxisName.Equals(hname))
                            {
                                posLimit = item.Position;
                            }
                            if (item.LimitType == LimitType.SoftNegativeLimit && item.LimitAxisName.Equals(hname))
                            {
                                negLimit = item.Position;
                            }
                        }
                        if (posLimit != 0 || negLimit != 0)
                        {
                            foreach (Control c in sTC_H.SelectedTab.AttachedControl.Controls)
                            {
                                (c as MotionBoard).SetXLimit(posLimit + 100, negLimit - 100);
                                (c as MotionBoard).UpdateBoard();
                                c.Refresh();
                            }
                        }
                    }
                }
            }
        }

        private void resizePanel()
        {
            if (this.Width > 0 && this.sC_LeftRight.Width > 0 && this.sC_UpDownOuter.Height > 0)
            {
                //this.sC_UpDown.SplitterDistance = this.sC_UpDown.Height * 7 / 10;
                this.sC_UpDownInner.SplitterDistance = this.sC_UpDownInner.Height / 2;
                this.sC_LeftRight.SplitterDistance = this.sC_LeftRight.Width * 13 / 20;
                this.sC_LeftRightDown.SplitterDistance = this.sC_LeftRightDown.Width * 1 / 2;
                this.sC_UpDownOuter.SplitterDistance = this.sC_UpDownOuter.Height * 7 / 10;

                if (!string.IsNullOrEmpty(XYNames) || !string.IsNullOrEmpty(DDName))
                {
                    if (HNames == null || HNames.Count == 0)
                    {
                        this.sC_UpDownInnerLeft.SplitterDistance = this.sC_UpDownInnerLeft.Height;
                    }
                    else
                    {
                        this.sC_UpDownInnerLeft.SplitterDistance = this.sC_UpDownInnerLeft.Height * 2 / 3;
                    }
                    if ((ZNames == null || ZNames.Count == 0) && (RNames == null || RNames.Count == 0))
                    {
                        this.sC_LeftRightInner.SplitterDistance = this.sC_LeftRightInner.Width;
                    }
                    else
                    {
                        this.sC_LeftRightInner.SplitterDistance = this.sC_LeftRightInner.Width * 3 / 5;
                        if (RNames == null || RNames.Count == 0)
                            this.sC_UpDownInner.SplitterDistance = this.sC_UpDownInner.Height;
                        if (ZNames == null || ZNames.Count == 0)
                            this.sC_UpDownInner.SplitterDistance = 0;
                    }
                }
                else
                {
                    if (HNames == null || HNames.Count == 0)
                    {
                        this.sC_LeftRightInner.SplitterDistance = 0;
                    }
                    else
                    {
                        this.sC_UpDownInnerLeft.SplitterDistance = 0;
                        this.sC_LeftRightInner.SplitterDistance = this.sC_LeftRightInner.Width * 1 / 2;
                    }
                    if (RNames == null || RNames.Count == 0)
                        this.sC_UpDownInner.SplitterDistance = this.sC_UpDownInner.Height;
                    if (ZNames == null || ZNames.Count == 0)
                        this.sC_UpDownInner.SplitterDistance = 0;
                }
            }
        }

        private void FormAxis_Resize(object sender, EventArgs e)
        {
            resizePanel();
        }

        public void AddZTab(string moduleName)
        {
            SuperTabItem superTabItem1 = new SuperTabItem();
            SuperTabControlPanel superTabControlPanel1 = new SuperTabControlPanel();
            // 
            // superTabItem1
            // 
            superTabItem1.AttachedControl = superTabControlPanel1;
            superTabItem1.GlobalItem = false;
            superTabItem1.Name = moduleName;
            superTabItem1.Text = moduleName;
            // 
            // superTabControlPanel1
            // 
            superTabControlPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            superTabControlPanel1.Name = "superTabControlPanel1";
            superTabControlPanel1.TabItem = superTabItem1;

            sTC_Z.Tabs.Add(superTabItem1);
            this.sTC_Z.Controls.Add(superTabControlPanel1);
        }
        public void AddHTab(string moduleName)
        {
            SuperTabItem superTabItem1 = new SuperTabItem();
            SuperTabControlPanel superTabControlPanel1 = new SuperTabControlPanel();
            // 
            // superTabItem1
            // 
            superTabItem1.AttachedControl = superTabControlPanel1;
            superTabItem1.GlobalItem = false;
            superTabItem1.Name = moduleName;
            superTabItem1.Text = moduleName;
            // 
            // superTabControlPanel1
            // 
            superTabControlPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            superTabControlPanel1.Name = "superTabControlPanel1";
            superTabControlPanel1.TabItem = superTabItem1;

            sTC_H.Tabs.Add(superTabItem1);
            this.sTC_H.Controls.Add(superTabControlPanel1);
        }
        public void AddRTab(string moduleName)
        {
            SuperTabItem superTabItem1 = new SuperTabItem();
            SuperTabControlPanel superTabControlPanel1 = new SuperTabControlPanel();
            // 
            // superTabItem1
            // 
            superTabItem1.AttachedControl = superTabControlPanel1;
            superTabItem1.GlobalItem = false;
            superTabItem1.Name = moduleName;
            superTabItem1.Text = moduleName;
            // 
            // superTabControlPanel1
            // 
            superTabControlPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            superTabControlPanel1.Name = "superTabControlPanel1";
            superTabControlPanel1.TabItem = superTabItem1;

            sTC_R.Tabs.Add(superTabItem1);
            this.sTC_R.Controls.Add(superTabControlPanel1);
        }
        public void createPointGrid()
        {
            DevComponents.DotNetBar.SuperGrid.GridColumn gridColumn6;
            gridColumn6 = new DevComponents.DotNetBar.SuperGrid.GridColumn();
            sGC_Point.PrimaryGrid.Columns.Add(gridColumn6);
            gridColumn6.CellStyles.Default.Alignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleCenter;
            gridColumn6.CellStyles.Default.AllowWrap = DevComponents.DotNetBar.SuperGrid.Style.Tbool.True;
            gridColumn6.DefaultNewRowCellValue = "Point Name";
            gridColumn6.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
            gridColumn6.HeaderStyles.Default.Image = Properties.Resources.名称16;
            gridColumn6.Name = "点位名称";
            gridColumn6.Width = 200;

            if (!string.IsNullOrEmpty(XYNames))
            {
                addAxisColumn(XYNames.Split(',')[0]);
                addAxisColumn(XYNames.Split(',')[1]);
            }
            if (HNames != null && HNames.Count > 0)
            {
                foreach (var item in HNames)
                {
                    addAxisColumn(item);
                }
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                foreach (var item in ZNames)
                {
                    addAxisColumn(item);
                }
            }
            if (RNames != null && RNames.Count > 0)
            {
                foreach (var item in RNames)
                {
                    addAxisColumn(item);
                }
            }

            DevComponents.DotNetBar.SuperGrid.GridColumn gotoPath = new DevComponents.DotNetBar.SuperGrid.GridColumn();
            sGC_Point.PrimaryGrid.Columns.Add(gotoPath);
            gotoPath.EnableGroupHeaderMarkup = true;
            gotoPath.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
            gotoPath.HeaderStyles.Default.Image = Properties.Resources.对象16;
            gotoPath.Name = "到达";
            gotoPath.ReadOnly = true;
            gotoPath.DefaultNewRowCellValue = "";
            gotoPath.Width = 100;

            DevComponents.DotNetBar.SuperGrid.GridColumn gridPath = new DevComponents.DotNetBar.SuperGrid.GridColumn();
            sGC_Point.PrimaryGrid.Columns.Add(gridPath);
            gridPath.EnableGroupHeaderMarkup = true;
            gridPath.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
            gridPath.HeaderStyles.Default.Image = Properties.Resources.路径16;
            gridPath.Name = "路径";
            gridPath.ReadOnly = true;
            gridPath.DefaultNewRowCellValue = "";
            gridPath.Width = 100;

            if (!string.IsNullOrEmpty(XYNames))
            {
                DevComponents.DotNetBar.SuperGrid.GridColumn girdX = new DevComponents.DotNetBar.SuperGrid.GridColumn();
                sGC_Point.PrimaryGrid.Columns.Add(girdX);
                girdX.EnableGroupHeaderMarkup = true;
                girdX.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
                girdX.HeaderStyles.Default.Image = Properties.Resources.路径16;
                girdX.Name = $"仅移动{XYNames.Split(',')[0]}";
                girdX.ReadOnly = true;
                girdX.DefaultNewRowCellValue = "";
                girdX.Width = 100;

                DevComponents.DotNetBar.SuperGrid.GridColumn gridY = new DevComponents.DotNetBar.SuperGrid.GridColumn();
                sGC_Point.PrimaryGrid.Columns.Add(gridY);
                gridY.EnableGroupHeaderMarkup = true;
                gridY.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
                gridY.HeaderStyles.Default.Image = Properties.Resources.路径16;
                gridY.Name = $"仅移动{XYNames.Split(',')[1]}";
                gridY.ReadOnly = true;
                gridY.DefaultNewRowCellValue = "";
                gridY.Width = 100;
            }
            if (HNames != null && HNames.Count > 0)
            {
                foreach (var item in HNames)
                {
                    DevComponents.DotNetBar.SuperGrid.GridColumn gridH = new DevComponents.DotNetBar.SuperGrid.GridColumn();
                    sGC_Point.PrimaryGrid.Columns.Add(gridH);
                    gridH.EnableGroupHeaderMarkup = true;
                    gridH.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
                    gridH.HeaderStyles.Default.Image = Properties.Resources.路径16;
                    gridH.Name = $"仅移动{item}";
                    gridH.ReadOnly = true;
                    gridH.DefaultNewRowCellValue = "";
                    gridH.Width = 100;
                }
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                foreach (var item in ZNames)
                {
                    DevComponents.DotNetBar.SuperGrid.GridColumn gridZ = new DevComponents.DotNetBar.SuperGrid.GridColumn();
                    sGC_Point.PrimaryGrid.Columns.Add(gridZ);
                    gridZ.EnableGroupHeaderMarkup = true;
                    gridZ.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
                    gridZ.HeaderStyles.Default.Image = Properties.Resources.路径16;
                    gridZ.Name = $"仅移动{item}";
                    gridZ.ReadOnly = true;
                    gridZ.DefaultNewRowCellValue = "";
                    gridZ.Width = 100;
                }
            }
            if (RNames != null && RNames.Count > 0)
            {
                foreach (var item in RNames)
                {
                    DevComponents.DotNetBar.SuperGrid.GridColumn gridR = new DevComponents.DotNetBar.SuperGrid.GridColumn();
                    sGC_Point.PrimaryGrid.Columns.Add(gridR);
                    gridR.EnableGroupHeaderMarkup = true;
                    gridR.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
                    gridR.HeaderStyles.Default.Image = Properties.Resources.路径16;
                    gridR.Name = $"仅移动{item}";
                    gridR.ReadOnly = true;
                    gridR.DefaultNewRowCellValue = "";
                    gridR.Width = 100;
                }
            }
        }
        private void Path_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void loadPoint()
        {
            List<string> allAxisName = new List<string>();
            List<string> allPointName = new List<string>();
            List<double> allPosition = new List<double>();
            List<double> hPos = new List<double>();
            List<double> zPos = new List<double>();
            List<double> rPos = new List<double>();
            if (!string.IsNullOrEmpty(XYNames))
            {
                allAxisName.Add(XYNames.Split(',')[0]);
                allAxisName.Add(XYNames.Split(',')[1]);
            }
            if (HNames != null && HNames.Count > 0)
                HNames.ForEach(name => { allAxisName.Add(name); });
            if (ZNames != null && ZNames.Count > 0)
                ZNames.ForEach(name => { allAxisName.Add(name); });
            if (RNames != null && RNames.Count > 0)
                RNames.ForEach(name => { allAxisName.Add(name); });
            foreach (var item in allAxisName)
            {
                foreach (var item2 in SingletonProvider<MachineResource>.Instance.MachineAxis[item].MotionPosition)
                {
                    if (!allPointName.Contains(item2.Key) && !item2.Key.Contains('-'))
                        allPointName.Add(item2.Key);
                }
            }

            foreach (var item in allPointName)
            {
                string trayName = "";
                List<double> pts = new List<double>();
                double xposOne = 0;
                double yposOne = 0;
                List<double> hPosOne = new List<double>();
                List<double> zPosOne = new List<double>();
                List<double> rPosOne = new List<double>();
                if (!string.IsNullOrEmpty(XYNames))
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].MotionPosition.ContainsKey(item))
                        xposOne = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].MotionPosition[item];
                    //xposOne = 100;
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].MotionPosition.ContainsKey(item))
                        yposOne = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].MotionPosition[item];
                    //yposOne = 100;
                }
                if (HNames != null && HNames.Count > 0)
                    HNames.ForEach(name =>
                    {
                        if (SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition.ContainsKey(item))
                            hPosOne.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition[item]);
                    });
                //HNames.ForEach(name => { hPosOne.Add(100); });
                if (ZNames != null && ZNames.Count > 0)
                    ZNames.ForEach(name =>
                    {
                        if (SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition.ContainsKey(item))
                            zPosOne.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition[item]);
                    });
                //ZNames.ForEach(name => { zPosOne.Add(100); });
                if (RNames != null && RNames.Count > 0)
                    RNames.ForEach(name =>
                    {
                        if (SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition.ContainsKey(item))
                            rPosOne.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition[item]);
                    });
                //RNames.ForEach(name => { rPosOne.Add(100); });

                allAxisName.ForEach(name =>
                {
                    if (SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition.ContainsKey(item))
                        pts.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].MotionPosition[item]);
                });
                if (allAxisName.Count != pts.Count)
                    continue;
                addPointRow(item, allAxisName, pts, false, trayName);
                refreshPointOnBoard(item, xposOne, yposOne, zPosOne, rPosOne, hPosOne);
            }
        }
        private void loadTray()
        {
            this.sGC_Tray.PrimaryGrid.Rows.Clear();
            if (MachineResource.Instance.ModuleTrays != null && MachineResource.Instance.ModuleTrays.Count > 0)
            {
                if (MachineResource.Instance.ModuleTrays.ContainsKey(_moduleName))
                {
                    foreach (var item in MachineResource.Instance.ModuleTrays[_moduleName])
                    {
                        addTrayRow(item.Value);
                        bool addZR = true;
                        for (int i = 0; i < item.Value.RowCount * item.Value.ColCount; i++)
                        {
                            PointF p = item.Value.Helper.GetPoint(i);
                            if (i > 0)
                                addZR = false;
                            refreshPointOnBoard($"{item.Value.Name}_{i}", p.X, p.Y, new List<double>() { item.Value.NozzleZ }, new List<double>() { item.Value.NozzleR }, null, "", addZR);
                        }
                    }
                }
            }
        }
        private void initDIOGrid()
        {
            this.sGC_Input.PrimaryGrid.Columns["状态"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_Input.PrimaryGrid.Columns["状态"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            this.sGC_Output.PrimaryGrid.Columns["原点"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_Output.PrimaryGrid.Columns["原点"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };

            this.sGC_Output.PrimaryGrid.Columns["动点"].EditorType = typeof(MyGridImageEditControl);
            this.sGC_Output.PrimaryGrid.Columns["动点"].EditorParams = new object[] { iL_Input, ImageSizeMode.CenterImage };
        }
        private void addInputColumn(string ioName, Sensor sensor)
        {
            GridRow gridRow = new GridRow();
            gridRow.Tag = sensor;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = sensor.Description;
            GridCell gridCell3 = new GridCell();
            gridCell3.Value = false;
            gridCell3.Visible = false;
            GridCell gridCell2 = new GridCell();
            gridCell2.Value = "圆形未选中32.png";
            gridCell2.ReadOnly = true;

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell3);
            gridRow.Cells.Add(gridCell2);
            this.sGC_Input.PrimaryGrid.Rows.Add(gridRow);
        }
        private void addOutputColumn(string ioName, Cylinder cylinder)
        {
            GridRow gridRow = new GridRow();
            gridRow.Tag = cylinder;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = cylinder.Description;
            GridCell gridCell3 = new GridCell();
            gridCell3.Value = cylinder.AlreadyOn();
            GridCell gridCell2 = new GridCell();
            gridCell2.Value = "圆形未选中32.png";
            gridCell2.ReadOnly = true;
            GridCell gridCell4 = new GridCell();
            gridCell4.Value = "圆形未选中32.png";
            gridCell4.ReadOnly = true;

            gridRow.Cells.Add(gridCell1);
            gridRow.Cells.Add(gridCell3);
            gridRow.Cells.Add(gridCell2);
            gridRow.Cells.Add(gridCell4);

            this.sGC_Output.PrimaryGrid.Rows.Add(gridRow);
        }
        private void addAxisColumn(string axisName)
        {
            DevComponents.DotNetBar.SuperGrid.GridColumn gridColumn20 = new DevComponents.DotNetBar.SuperGrid.GridColumn();
            sGC_Point.PrimaryGrid.Columns.Add(gridColumn20);
            gridColumn20.EditorType = typeof(DevComponents.DotNetBar.SuperGrid.GridDoubleInputEditControl);
            gridColumn20.EnableGroupHeaderMarkup = true;
            gridColumn20.HeaderStyles.Default.ImageAlignment = DevComponents.DotNetBar.SuperGrid.Style.Alignment.MiddleLeft;
            gridColumn20.HeaderStyles.Default.Image = Properties.Resources.电机;
            gridColumn20.Name = axisName;
            gridColumn20.DefaultNewRowCellValue = "0.0";
            gridColumn20.Width = 80;
        }

        public void addPointRow(string name, List<string> allAxis, List<double> allPosition, bool isAdd = true, string trayName = "")
        {
            GridRow gridRow = new GridRow();
            gridRow.Tag = name;

            GridCell gridCell1 = new GridCell();
            gridCell1.ReadOnly = !string.IsNullOrEmpty(trayName);
            gridCell1.Value = name;

            gridRow.Cells.Add(gridCell1);
            for (int i = 0; i < allAxis.Count; i++)
            {
                if (i < allPosition.Count)
                {
                    GridCell gridAxis = new GridCell();
                    double pos = allPosition[i];
                    gridAxis.Value = pos.ToString("0.000");
                    if (isAdd)
                    {
                        SingletonProvider<MachineResource>.Instance.MachineAxis[allAxis[i]].MotionPosition.Add(name, pos);
                    }
                    gridRow.Cells.Add(gridAxis);
                }
            }

            GridCell buttonCell2 = new GridCell();
            buttonCell2.Value = "运动至此";
            gridRow.Cells.Add(buttonCell2);

            GridCell buttonCell = new GridCell();
            buttonCell.Value = "设定路径";
            gridRow.Cells.Add(buttonCell);

            if (!string.IsNullOrEmpty(XYNames))
            {
                GridCell gridAxis = new GridCell();
                gridAxis.Value = "移动";
                gridRow.Cells.Add(gridAxis);
                GridCell gridAxisY = new GridCell();
                gridAxisY.Value = "移动";
                gridRow.Cells.Add(gridAxisY);
                for (int i = 2; i < allAxis.Count; i++)
                {
                    GridCell gridAxisOther = new GridCell();
                    gridAxisOther.Value = "移动";
                    gridRow.Cells.Add(gridAxisOther);
                }
            }
            else
            {
                for (int i = 0; i < allAxis.Count; i++)
                {
                    GridCell gridAxisOther = new GridCell();
                    gridAxisOther.Value = "移动";
                    gridRow.Cells.Add(gridAxisOther);
                }
            }

            this.sGC_Point.PrimaryGrid.Rows.Add(gridRow);
        }
        public void editPointRow(GridRow row, string name, List<string> allAxis, List<double> allPosition, bool isAdd = true, string trayName = "")
        {
            for (int i = 0; i < allAxis.Count; i++)
            {
                if (i < allPosition.Count)
                {
                    GridCell gridAxis = new GridCell();
                    double pos = allPosition[i];
                    gridAxis.Value = pos.ToString("0.000");
                    SingletonProvider<MachineResource>.Instance.MachineAxis[allAxis[i]].MotionPosition[name] = pos;
                    row.Cells[allAxis[i]].Value = pos;
                }
            }
        }
        public void addTrayRow(Tray tray)
        {
            GridRow gridRow = new GridRow();
            gridRow.Tag = tray;

            GridCell gridCell1 = new GridCell();
            gridCell1.Value = tray.Name;
            gridRow.Cells.Add(gridCell1);

            GridCell buttonCell = new GridCell();
            buttonCell.Value = tray.RowCount.ToString();
            gridRow.Cells.Add(buttonCell);

            GridCell buttonCell2 = new GridCell();
            buttonCell2.Value = tray.ColCount.ToString();
            gridRow.Cells.Add(buttonCell2);

            GridCell buttonCell3 = new GridCell();
            buttonCell3.Value = tray.NozzleCount.ToString();
            gridRow.Cells.Add(buttonCell3);

            GridCell buttonCell4 = new GridCell();
            buttonCell4.Value = tray.OffsetX.ToString("0.000");
            gridRow.Cells.Add(buttonCell4);

            GridCell buttonCell5 = new GridCell();
            buttonCell5.Value = tray.OffsetY.ToString("0.000");
            gridRow.Cells.Add(buttonCell5);

            GridCell buttonCell8 = new GridCell();
            buttonCell8.Value = tray.NozzleZ.ToString("0.000");
            gridRow.Cells.Add(buttonCell8);

            GridCell buttonCell9 = new GridCell();
            buttonCell9.Value = tray.NozzleR.ToString("0.000");
            gridRow.Cells.Add(buttonCell9);

            GridCell buttonCell6 = new GridCell();
            buttonCell6.Value = "设定路径";
            gridRow.Cells.Add(buttonCell6);

            GridCell buttonCell7 = new GridCell();
            buttonCell7.Value = "运动至此";
            gridRow.Cells.Add(buttonCell7);

            this.sGC_Tray.PrimaryGrid.Rows.Add(gridRow);
        }
        public void editTrayRow(GridRow row, Tray tray)
        {
            row.Tag = tray;
            row.Cells[0].Value = tray.Name;
            row.Cells[1].Value = tray.RowCount;
            row.Cells[2].Value = tray.ColCount;
            row.Cells[3].Value = tray.NozzleCount;
            row.Cells[4].Value = tray.OffsetX.ToString("0.000");
            row.Cells[5].Value = tray.OffsetY.ToString("0.000");
            row.Cells[6].Value = tray.NozzleZ.ToString("0.000");
            row.Cells[7].Value = tray.NozzleR.ToString("0.000");
        }
        bool isAdding = false;
        private void 添加当前点位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            isAdding = true;
            List<string> allAxisName = new List<string>();
            List<string> allPointName = new List<string>();
            List<double> allPosition = new List<double>();
            double xpos = 0;
            double ypos = 0;
            List<double> hPos = new List<double>();
            List<double> zPos = new List<double>();
            List<double> rPos = new List<double>();
            if (!string.IsNullOrEmpty(XYNames))
            {
                allAxisName.Add(XYNames.Split(',')[0]);
                xpos = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].Position;
                allAxisName.Add(XYNames.Split(',')[1]);
                ypos = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].Position;
            }
            if (HNames != null && HNames.Count > 0)
                HNames.ForEach(name => { allAxisName.Add(name); hPos.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position); });
            if (ZNames != null && ZNames.Count > 0)
                ZNames.ForEach(name => { allAxisName.Add(name); zPos.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position); });
            if (RNames != null && RNames.Count > 0)
                RNames.ForEach(name => { allAxisName.Add(name); rPos.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position); });
            foreach (var item in allAxisName)
            {
                foreach (var item2 in SingletonProvider<MachineResource>.Instance.MachineAxis[item].MotionPosition.Keys)
                {
                    if (!allPointName.Contains(item2))
                        allPointName.Add(item2);
                }
            }
            string tempName = getDefaultPointName(allPointName);
            allAxisName.ForEach(a => allPosition.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[a].Position));
            //allAxisName.ForEach(a => allPosition.Add(100));
            //if(sGC_Point.GetSelectedRows().Count > 0)
            //    editPointRow(sGC_Point.GetSelectedRows()[0] as GridRow, tempName, allAxisName, allPosition);
            //else
            addPointRow(tempName, allAxisName, allPosition);
            refreshPointOnBoard(tempName, xpos, ypos, zPos, rPos, hPos);
            //for (int i = 0; i < allAxisName.Count; i++)
            //{
            //    MachineResource.Instance.MachineAxis[allAxisName[i]].MotionPosition.Add(tempName, allPosition[i]);  
            //}
            MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
            isAdding = false;
        }
        private string getDefaultPointName(List<string> allNames)
        {
            for (int i = 0; i < 9999; i++)
            {
                if (!allNames.Contains($"PointName{i}"))
                    return $"PointName{i}";
            }
            return null;
        }

        private void 添加自定义点位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> allAxisName = new List<string>();
            List<string> allPointName = new List<string>();
            List<double> allPosition = new List<double>();
            double xpos = 0;
            double ypos = 0;
            List<double> hPos = new List<double>();
            List<double> zPos = new List<double>();
            List<double> rPos = new List<double>();
            if (!string.IsNullOrEmpty(XYNames))
            {
                allAxisName.Add(XYNames.Split(',')[0]);
                xpos = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].Position;
                allAxisName.Add(XYNames.Split(',')[1]);
                ypos = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].Position;
            }
            if (HNames != null && HNames.Count > 0)
                HNames.ForEach(name => { allAxisName.Add(name); hPos.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position); });
            if (ZNames != null && ZNames.Count > 0)
                ZNames.ForEach(name => { allAxisName.Add(name); zPos.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position); });
            if (RNames != null && RNames.Count > 0)
                RNames.ForEach(name => { allAxisName.Add(name); rPos.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[name].Position); });
            foreach (var item in allAxisName)
            {
                foreach (var item2 in SingletonProvider<MachineResource>.Instance.MachineAxis[item].MotionPosition.Keys)
                {
                    if (!allPointName.Contains(item2))
                        allPointName.Add(item2);
                }
            }
            string tempName = getDefaultPointName(allPointName);
            allAxisName.ForEach(a => allPosition.Add(SingletonProvider<MachineResource>.Instance.MachineAxis[a].Position));
            //allAxisName.ForEach(a => allPosition.Add(100));
            //if (sGC_Point.GetSelectedRows().Count > 0)
            editPointRow(sGC_Point.GetSelectedRows()[0] as GridRow, (sGC_Point.GetSelectedRows()[0] as GridRow).Cells[0].Value.ToString(), allAxisName, allPosition);
            //else
            //addPointRow(tempName, allAxisName, allPosition);
            refreshPointOnBoard(tempName, xpos, ypos, zPos, rPos, hPos);
            //for (int i = 0; i < allAxisName.Count; i++)
            //{
            //    MachineResource.Instance.MachineAxis[allAxisName[i]].MotionPosition.Add(tempName, allPosition[i]);  
            //}
            MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
        }
        private void refreshPointOnBoard(string pointName, double xpos, double ypos, List<double> zpos, List<double> rpos, List<double> hpos, string oldName = "", bool addZR = true)
        {
            List<double> pos;
            if (!string.IsNullOrEmpty(XYNames))
            {
                if (!string.IsNullOrEmpty(oldName))
                {
                    this.xy_Displayer.MotionPoints.RemoveAll(p => p.Description == oldName);
                    this.xy_Displayer.CreatedShapes.RemoveAll(p => p.Description == oldName);
                }
                this.xy_Displayer.MotionPoints.RemoveAll(p => p.Description == pointName);
                this.xy_Displayer.CreatedShapes.RemoveAll(p => p.Description == pointName);
                MotionPoint mp = new MotionPoint(pointName);
                mp.OriginPoint = new List<float>() { (float)xpos, (float)ypos, 0 };
                pos = this.xy_Displayer.GetBoardPosition(xpos, ypos, 0);
                mp.CenterPoint = new PointF((float)pos[0], (float)pos[1]);
                mp.PaintColor = Color.Tomato;
                mp.LineWidth = 2;
                mp.Description = pointName;
                this.xy_Displayer.CreatedShapes.Add(mp);
                this.xy_Displayer.MotionPoints.Add(mp);
                this.xy_Displayer.Repaint();
            }
            if (addZR)
            {
                if (ZNames != null && ZNames.Count > 0 && zpos.Count > 0)
                {
                    int index = 0;
                    foreach (SuperTabItem tp in sTC_Z.Tabs)
                    {
                        foreach (Control item in tp.AttachedControl.Controls)
                        {
                            if (item is MotionBoard)
                            {
                                if (!string.IsNullOrEmpty(oldName))
                                {
                                    (item as MotionBoard).MotionPoints.RemoveAll(p => p.Description == oldName);
                                    (item as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == oldName);
                                }
                                (item as MotionBoard).MotionPoints.RemoveAll(p => p.Description == pointName);
                                (item as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == pointName);

                                MotionPoint mp = new MotionPoint(pointName);
                                mp.OriginPoint = new List<float>() { 0, (float)zpos[index], 0 };
                                pos = (item as MotionBoard).GetBoardPosition(0, (float)zpos[index], 0);
                                mp.CenterPoint = new PointF((float)pos[0], (float)pos[1]);
                                mp.PaintColor = Color.Tomato;
                                mp.LineWidth = 2;
                                mp.Description = pointName;
                                (item as MotionBoard).CreatedShapes.Add(mp);
                                (item as MotionBoard).MotionPoints.Add(mp);
                                (item as MotionBoard).Repaint();
                                break;
                            }
                        }
                        index++;
                    }
                }
                if (HNames != null && HNames.Count > 0 && hpos != null && hpos.Count > 0)
                {
                    int index = 0;
                    foreach (SuperTabItem tp in sTC_H.Tabs)
                    {
                        foreach (Control item in tp.AttachedControl.Controls)
                        {
                            if (item is MotionBoard)
                            {
                                if (!string.IsNullOrEmpty(oldName))
                                {
                                    (item as MotionBoard).MotionPoints.RemoveAll(p => p.Description == oldName);
                                    (item as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == oldName);
                                }
                                (item as MotionBoard).MotionPoints.RemoveAll(p => p.Description == pointName);
                                (item as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == pointName);

                                MotionPoint mp = new MotionPoint(pointName);
                                mp.OriginPoint = new List<float>() { (float)hpos[index], 0, 0 };
                                pos = (item as MotionBoard).GetBoardPosition((float)hpos[index], 0, 0);
                                mp.CenterPoint = new PointF((float)pos[0], (float)pos[1]);
                                mp.PaintColor = Color.Tomato;
                                mp.LineWidth = 2;
                                mp.Description = pointName;
                                (item as MotionBoard).CreatedShapes.Add(mp);
                                (item as MotionBoard).MotionPoints.Add(mp);
                                (item as MotionBoard).Repaint();
                                break;
                            }
                        }
                        index++;
                    }
                }
                if (RNames != null && RNames.Count > 0 && rpos.Count > 0)
                {
                    int index = 0;
                    foreach (SuperTabItem tp in sTC_R.Tabs)
                    {
                        foreach (Control item in tp.AttachedControl.Controls)
                        {
                            if (item is MotionBoard)
                            {
                                if (!string.IsNullOrEmpty(oldName))
                                {
                                    (item as MotionBoard).MotionPoints.RemoveAll(p => p.Description == oldName);
                                    (item as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == oldName);
                                }
                                (item as MotionBoard).MotionPoints.RemoveAll(p => p.Description == pointName);
                                (item as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == pointName);

                                MotionPoint mp = new MotionPoint(pointName);
                                mp.OriginPoint = new List<float>() { 0, 0, (float)rpos[index] };
                                pos = (item as MotionBoard).GetBoardPosition(0, 0, (float)rpos[index]);
                                mp.CenterPoint = new PointF((float)pos[2], (float)pos[3]);
                                mp.PaintColor = Color.Tomato;
                                mp.LineWidth = 2;
                                mp.Description = pointName;
                                (item as MotionBoard).CreatedShapes.Add(mp);
                                (item as MotionBoard).MotionPoints.Add(mp);
                                (item as MotionBoard).Repaint();
                            }
                            break;
                        }
                        index++;
                    }
                }
            }
        }
        private void refreshAllBoard()
        {
            if (!string.IsNullOrEmpty(XYNames))
            {
                this.xy_Displayer.Repaint();
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                int index = 0;
                foreach (SuperTabItem tp in sTC_Z.Tabs)
                {
                    foreach (Control item in tp.AttachedControl.Controls)
                    {
                        if (item is MotionBoard)
                        {
                            (item as MotionBoard).Repaint();
                        }
                    }
                    index++;
                }
            }
            if (RNames != null && RNames.Count > 0)
            {
                int index = 0;
                foreach (SuperTabItem tp in sTC_R.Tabs)
                {
                    foreach (Control item in tp.AttachedControl.Controls)
                    {
                        if (item is MotionBoard)
                        {
                            (item as MotionBoard).Repaint();
                        }
                    }
                    index++;
                }
            }
        }
        private void deletePointOnBoard(string name)
        {
            if (xy_Displayer != null)
            {
                this.xy_Displayer.MotionPoints.RemoveAll(p => p.Description == name);
                this.xy_Displayer.CreatedShapes.RemoveAll(p => p.Description == name);
                foreach (SuperTabItem item in sTC_Z.Tabs)
                {
                    foreach (Control c in (item.AttachedControl as SuperTabControlPanel).Controls)
                    {
                        if (c is MotionBoard)
                        {
                            (c as MotionBoard).MotionPoints.RemoveAll(p => p.Description == name);
                            (c as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == name);
                        }
                    }
                }
                foreach (SuperTabItem item in sTC_R.Tabs)
                {
                    foreach (Control c in (item.AttachedControl as SuperTabControlPanel).Controls)
                    {
                        if (c is MotionBoard)
                        {
                            (c as MotionBoard).MotionPoints.RemoveAll(p => p.Description == name);
                            (c as MotionBoard).CreatedShapes.RemoveAll(p => p.Description == name);
                        }
                    }
                }
            }
        }
        private void 删除点位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sGC_Point.GetSelectedRows().Count == 0)
            {
                SystemInfo.ShowToastInfo(this, "请先选中需要删除的点位", true);
                return;
            }
            if (sGC_Point.GetSelectedRows().Count > 1)
            {
                if (MessageBox.Show("确定批量删除多个点位吗?", "删除点位", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    List<string> axisNames = GetCurrentAxiss();
                    foreach (var row in sGC_Point.GetSelectedRows())
                    {
                        foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis)
                        {
                            if (!axisNames.Contains(item.Key))
                                continue;
                            string name = (row as GridRow).Cells[0].Value.ToString();
                            item.Value.MotionPosition.Remove(name);
                            deletePointOnBoard(name);
                        }
                        refreshAllBoard();
                        sGC_Point.PrimaryGrid.Rows.Remove(row);
                    }
                    MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                }
            }
            else
            {
                if (MessageBox.Show("确定删除该点位吗?", "删除点位", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    List<string> axisNames = GetCurrentAxiss();
                    foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis)
                    {
                        if (!axisNames.Contains(item.Key))
                            continue;
                        string name = (sGC_Point.GetSelectedRows()[0] as GridRow).Cells[0].Value.ToString();
                        item.Value.MotionPosition.Remove(name);
                        deletePointOnBoard(name);
                    }
                    refreshAllBoard();
                    MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                    sGC_Point.PrimaryGrid.Rows.Remove(sGC_Point.GetSelectedRows()[0]);
                    //if(XYNames != null)
                }
            }
        }
        public List<string> GetCurrentAxiss()
        {
            List<string> names = new List<string>();
            if (!string.IsNullOrEmpty(XYNames))
            {
                names.Add(XYNames.Split(',')[0]);
                names.Add(XYNames.Split(',')[1]);
            }
            if (HNames != null && HNames.Count > 0)
            {
                foreach (var item in HNames)
                {
                    names.Add(item);
                }
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                foreach (var item in ZNames)
                {
                    names.Add(item);
                }
            }
            if (RNames != null && RNames.Count > 0)
            {
                foreach (var item in RNames)
                {
                    names.Add(item);
                }
            }
            return names;
        }

        private void sGC_Point_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            if (isAdding)
                return;
            //修改名字
            List<string> allaxisnames = new List<string>();
            if (e.GridCell.ColumnIndex == 0)
            {
                string oldName = e.GridCell.GridRow.Tag as string;
                string pointName = e.GridCell.GridRow.Cells[0].Value.ToString();
                if (!string.IsNullOrEmpty(XYNames))
                {
                    allaxisnames.Add(XYNames.Split(',')[0]);
                    allaxisnames.Add(XYNames.Split(',')[1]);
                    if (MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].MotionPosition.ContainsKey(pointName))
                    {
                        SystemInfo.ShowToastInfo(this, "已存在同名点位,请选择其他的名字作为点位名称", true);
                        return;
                    }
                    if (MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].MotionPosition.ContainsKey(pointName))
                    {
                        SystemInfo.ShowToastInfo(this, "已存在同名点位,请选择其他的名字作为点位名称", true);
                        return;
                    }
                    MotionPoint mp = xy_Displayer.MotionPoints.FirstOrDefault(p => p.Description == oldName);
                    if (mp != null)
                    {
                        mp.Description = pointName;
                        xy_Displayer.UpdateBoard();
                        panelXY.Invalidate();
                    }
                }
                if (HNames != null && HNames.Count > 0)
                {
                    HNames.ForEach(name =>
                    {
                        allaxisnames.Add(name);
                        if (MachineResource.Instance.MachineAxis[name].MotionPosition.ContainsKey(pointName))
                        {
                            SystemInfo.ShowToastInfo(this, "已存在同名点位,请选择其他的名字作为点位名称", true);
                            return;
                        }
                    });
                    foreach (SuperTabItem item in sTC_H.Tabs)
                    {
                        MotionBoard board = (item.AttachedControl as SuperTabControlPanel).Controls[0] as MotionBoard;
                        MotionPoint mp = board.MotionPoints.FirstOrDefault(p => p.Description == oldName);
                        if (mp != null)
                        {
                            mp.Description = pointName;
                            board.UpdateBoard();
                        }
                    }
                }
                if (ZNames != null && ZNames.Count > 0)
                {
                    ZNames.ForEach(name =>
                    {
                        allaxisnames.Add(name);
                        if (MachineResource.Instance.MachineAxis[name].MotionPosition.ContainsKey(pointName))
                        {
                            SystemInfo.ShowToastInfo(this, "已存在同名点位,请选择其他的名字作为点位名称", true);
                            return;
                        }
                    });
                    foreach (SuperTabItem item in sTC_Z.Tabs)
                    {
                        MotionBoard board = (item.AttachedControl as SuperTabControlPanel).Controls[0] as MotionBoard;
                        MotionPoint mp = board.MotionPoints.FirstOrDefault(p => p.Description == oldName);
                        if (mp != null)
                        {
                            mp.Description = pointName;
                            board.UpdateBoard();
                        }
                    }
                }
                if (RNames != null && RNames.Count > 0)
                {
                    RNames.ForEach(name =>
                    {
                        allaxisnames.Add(name);
                        if (MachineResource.Instance.MachineAxis[name].MotionPosition.ContainsKey(pointName))
                        {
                            SystemInfo.ShowToastInfo(this, "已存在同名点位,请选择其他的名字作为点位名称", true);
                            return;
                        }
                    });
                    foreach (SuperTabItem item in sTC_R.Tabs)
                    {
                        MotionBoard board = (item.AttachedControl as SuperTabControlPanel).Controls[0] as MotionBoard;
                        MotionPoint mp = board.MotionPoints.FirstOrDefault(p => p.Description == oldName);
                        if (mp != null)
                        {
                            mp.Description = pointName;
                            board.UpdateBoard();
                        }
                    }
                }
                foreach (var item in SingletonProvider<MachineResource>.Instance.MachineAxis)
                {
                    if (allaxisnames.Contains(item.Value.Description) && item.Value.MotionPosition.ContainsKey(oldName))
                    {
                        double pos = item.Value.MotionPosition[oldName];
                        item.Value.MotionPosition.Remove(oldName);
                        item.Value.MotionPosition.Add(pointName, pos);
                    }
                }
                e.GridCell.GridRow.Tag = pointName;
                //XmlSerializerHelper<MachineResource>.SerializeToFile(SingletonProvider<MachineResource>.Instance, PathHelper.GetConfigPath("Machine") + Properties.Settings.Default.ConfigName);
            }
            else
            {
                if (e.GridCell.GridColumn.Name.Equals("路径") ||
                    e.GridCell.GridColumn.Name.Equals("到达"))
                {

                }
                else if (e.GridCell.GridColumn.Name.Contains("仅移动"))
                {

                }
                else
                {
                    string axisName = sGC_Point.PrimaryGrid.Columns[e.GridCell.ColumnIndex].Name;
                    string pointName = e.GridCell.GridRow.Cells[0].Value.ToString();
                    SingletonProvider<MachineResource>.Instance.MachineAxis[axisName].MotionPosition[pointName] = Convert.ToDouble(e.GridCell.Value.ToString());
                    //XmlSerializerHelper<MachineResource>.SerializeToFile(SingletonProvider<MachineResource>.Instance, PathHelper.GetConfigPath("Machine") + Properties.Settings.Default.ConfigName);

                    List<string> allAxisName = new List<string>();
                    List<string> allPointName = new List<string>();
                    List<double> allPosition = new List<double>();
                    double xpos = 0;
                    double ypos = 0;
                    List<double> hPos = new List<double>();
                    List<double> zPos = new List<double>();
                    List<double> rPos = new List<double>();
                    if (!string.IsNullOrEmpty(XYNames))
                    {
                        allAxisName.Add(XYNames.Split(',')[0]);
                        xpos = Convert.ToDouble(e.GridCell.GridRow.Cells[1].Value.ToString());
                        allAxisName.Add(XYNames.Split(',')[1]);
                        ypos = Convert.ToDouble(e.GridCell.GridRow.Cells[2].Value.ToString());
                    }
                    if (HNames != null && HNames.Count > 0)
                    {
                        int index = string.IsNullOrEmpty(XYNames) ? 0 : 2;
                        HNames.ForEach(name =>
                        {
                            allAxisName.Add(name);
                            hPos.Add(Convert.ToDouble(e.GridCell.GridRow.Cells[index + 1].Value.ToString()));
                            index++;
                        });
                    }
                    if (ZNames != null && ZNames.Count > 0)
                    {
                        int index = 0;
                        if (HNames == null || HNames.Count == 0)
                            index = string.IsNullOrEmpty(XYNames) ? 0 : 2;
                        else
                            index = string.IsNullOrEmpty(XYNames) ? 0 + HNames.Count : 2 + HNames.Count;
                        ZNames.ForEach(name =>
                        {
                            allAxisName.Add(name);
                            zPos.Add(Convert.ToDouble(e.GridCell.GridRow.Cells[index + 1].Value.ToString()));
                            index++;
                        });
                    }
                    if (RNames != null && RNames.Count > 0)
                    {
                        int index = 0;
                        if (HNames == null || HNames.Count == 0)
                            index = string.IsNullOrEmpty(XYNames) ? 0 : 2;
                        else
                            index = string.IsNullOrEmpty(XYNames) ? 0 + HNames.Count : 2 + HNames.Count;
                        int index2 = 0;
                        if (ZNames == null || ZNames.Count == 0)
                            index2 = index;
                        else
                            index2 = index + ZNames.Count;
                        RNames.ForEach(name =>
                        {
                            allAxisName.Add(name);
                            rPos.Add(Convert.ToDouble(e.GridCell.GridRow.Cells[index2 + 1].Value.ToString()));
                            index2++;
                        });
                    }
                    foreach (var item in allAxisName)
                    {
                        foreach (var item2 in SingletonProvider<MachineResource>.Instance.MachineAxis[item].MotionPosition.Keys)
                        {
                            if (!allPointName.Contains(item2))
                                allPointName.Add(item2);
                        }
                    }
                    refreshPointOnBoard(pointName, xpos, ypos, zPos, rPos, hPos);
                }
            }
        }

        bool ContiMove = true;
        double MoveStep = 1;
        private void tSB_Mode_Click(object sender, EventArgs e)
        {
            tSB_Mode.Checked = !tSB_Mode.Checked;
            if (tSB_Mode.Checked)
            {
                tSB_Mode.Text = "寸动";
                tSB_Mode.Image = Properties.Resources.寸动;
                ContiMove = false;
                //tSB_Mode.BackColor = Color.YellowGreen;
                tSDDB_Step.Enabled = true;
                tSL_Step.Enabled = true;
            }
            else
            {
                tSB_Mode.Text = "连续";
                tSB_Mode.Image = Properties.Resources.连续;
                ContiMove = true;
                //tSB_Mode.BackColor = Color.BlueViolet;
                tSDDB_Step.Enabled = false;
                tSL_Step.Enabled = false;
            }
        }

        private void tSMI_1_Click(object sender, EventArgs e)
        {
            foreach (var item in tSDDB_Step.DropDownItems)
            {
                (item as ToolStripMenuItem).Checked = false;
            }
            if ((sender as ToolStripMenuItem).Name.Equals("tSMI_005"))
            {
                MoveStep = 0.05;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_01"))
            {
                MoveStep = 0.1;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_02"))
            {
                MoveStep = 0.2;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_05"))
            {
                MoveStep = 0.5;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_1"))
            {
                MoveStep = 1;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_2"))
            {
                MoveStep = 2;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_5"))
            {
                MoveStep = 5;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_10"))
            {
                MoveStep = 10;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_20"))
            {
                MoveStep = 20;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_50"))
            {
                MoveStep = 50;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("tSMI_100"))
            {
                MoveStep = 100;
            }
            (sender as ToolStripMenuItem).Checked = true;
            this.tSL_Step.Text = $"寸动:{MoveStep.ToString().PadLeft(5, ' ')}mm";
        }
        List<double> _trayZPos;
        List<double> _trayRPos;
        bool noEdit = false;
        Tray _editTray = null;
        private void tSMI_TrayPointClick(object sender, EventArgs e)
        {
            if ((sender as ToolStripMenuItem).Name.Equals("左上ToolStripMenuItem"))
            {
                List<string> names = new List<string>();
                foreach (var item in sGC_Point.PrimaryGrid.Rows)
                {
                    names.Add((item as GridRow).Cells[0].Value.ToString());
                }
                string zName = ZNames == null || ZNames.Count == 0 ? "" : ZNames[0];
                string rName = RNames == null || RNames.Count == 0 ? "" : RNames[0];
                //if (sGC_Tray.GetSelectedRows().Count == 0)
                _editTray = null;
                //else
                //    _editTray = sGC_Tray.GetSelectedRows()[0].Tag as Tray;
                FormTray trayForm = new FormTray(this.XYNames, zName, rName, _moduleName, _editTray);
                if (trayForm.ShowDialog() == DialogResult.OK)
                {
                    _editTray = trayForm.EditTray;
                    _trayZPos = new List<double>();
                    _trayRPos = new List<double>();
                    foreach (SuperTabItem item in sTC_Z.Tabs)
                    {
                        foreach (Control c in (item.AttachedControl as SuperTabControlPanel).Controls)
                        {
                            if (c is MotionBoard)
                            {
                                _trayZPos.Add((c as MotionBoard).Table.YPosition);
                            }
                        }
                    }
                    foreach (SuperTabItem item in sTC_R.Tabs)
                    {
                        foreach (Control c in (item.AttachedControl as SuperTabControlPanel).Controls)
                        {
                            if (c is MotionBoard)
                            {
                                _trayRPos.Add((c as MotionBoard).Table.RPosition);
                            }
                        }
                    }
                    refreshPointOnBoard($"{_editTray.Name}_左上", this.xy_Displayer.Table.XPosition, this.xy_Displayer.Table.YPosition, _trayZPos, _trayRPos, null);
                    //_editTray.LeftUpPoint = new PointF((float)this.xy_Displayer.Table.XPosition, (float)this.xy_Displayer.Table.YPosition);
                    _editTray.LeftUpPoint = new PointF((float)MachineResource.Instance.MachineAxis[_editTray.XAxisName].Position, (float)MachineResource.Instance.MachineAxis[_editTray.YAxisName].Position);
                    _editTray.IsLeftUpSet = true;
                    _editTray.NozzleZ = _trayZPos[0];
                    _editTray.NozzleR = _trayRPos[0];
                }
                //string name = getNewDefaultName(currentCrd.Dgv_Points);

                //addOnePoint(currentCrd.Dgv_Points, name);
                //dGVData2Table(currentCrd.Dgv_Points, currentCrd.PointsTable);
                //if (NotifyCoordinateChanged != null)
                //    NotifyCoordinateChanged(_allCoordinates);
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("右上ToolStripMenuItem"))
            {
                //if(!_isEditTrayBegin)
                //{
                //    MessageBox.Show("请先设置左上角以开始矩阵点位编辑", "矩阵点位", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    return;
                //}
                refreshPointOnBoard($"{_editTray.Name}_右上", this.xy_Displayer.Table.XPosition, this.xy_Displayer.Table.YPosition, _trayZPos, _trayRPos, null);
                //_editTray.RightUpPoint = new PointF((float)this.xy_Displayer.Table.XPosition, (float)this.xy_Displayer.Table.YPosition);
                _editTray.RightUpPoint = new PointF((float)MachineResource.Instance.MachineAxis[_editTray.XAxisName].Position, (float)MachineResource.Instance.MachineAxis[_editTray.YAxisName].Position);
                _editTray.IsRightUpSet = true;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("左下ToolStripMenuItem"))
            {
                refreshPointOnBoard($"{_editTray.Name}_左下", this.xy_Displayer.Table.XPosition, this.xy_Displayer.Table.YPosition, _trayZPos, _trayRPos, null);
                //_editTray.LeftDownPoint = new PointF((float)this.xy_Displayer.Table.XPosition, (float)this.xy_Displayer.Table.YPosition);
                _editTray.LeftDownPoint = new PointF((float)MachineResource.Instance.MachineAxis[_editTray.XAxisName].Position, (float)MachineResource.Instance.MachineAxis[_editTray.YAxisName].Position);
                _editTray.IsLeftDownSet = true;
            }
            else if ((sender as ToolStripMenuItem).Name.Equals("右下ToolStripMenuItem"))
            {
                if (_editTray.RowCount == 1 || _editTray.ColCount == 1)
                {

                }
                else
                {
                    if (!_editTray.IsLeftUpSet || !_editTray.IsRightUpSet || !_editTray.IsLeftDownSet)
                    {
                        MessageBox.Show("点位未设置完成，请按顺序设置", "矩阵点位", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                deletePointOnBoard($"{_editTray.Name}_左上");
                deletePointOnBoard($"{_editTray.Name}_右上");
                deletePointOnBoard($"{_editTray.Name}_左下");
                //refreshPointOnBoard($"{_editTray.Name}_右下", this.mB_Displayer.Table.XPosition, this.mB_Displayer.Table.YPosition, _trayZPos, _trayRPos);
                //_editTray.RightDownPoint = new PointF((float)this.xy_Displayer.Table.XPosition, (float)this.xy_Displayer.Table.YPosition);
                _editTray.RightDownPoint = new PointF((float)MachineResource.Instance.MachineAxis[_editTray.XAxisName].Position, (float)MachineResource.Instance.MachineAxis[_editTray.YAxisName].Position);
                _editTray.IsRightDownSet = true;
                _editTray.Helper.Calibrate();
                noEdit = true;
                bool addZR = true;
                for (int i = 0; i < _editTray.RowCount * _editTray.ColCount; i++)
                {
                    PointF p = _editTray.Helper.GetPoint(i);
                    if (i > 0)
                        addZR = false;
                    refreshPointOnBoard($"{_editTray.Name}_{i}", p.X, p.Y, _trayZPos, _trayRPos, null, "", addZR);
                }
                noEdit = false;
                addTrayRow(_editTray);
                if (!MachineResource.Instance.ModuleTrays.ContainsKey(_moduleName))
                    MachineResource.Instance.ModuleTrays.Add(_moduleName, new SerializableDictionary<string, Tray>());
                MachineResource.Instance.ModuleTrays[_moduleName].Add(_editTray.Name, _editTray);
                MotionFactory.Instance.InitializeTray(MachineResource.Instance);
                MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
            }
        }
        private List<string> getAllPointsName(bool hasTray = false)
        {
            List<string> pointNames = new List<string>();
            foreach (var item in sGC_Point.PrimaryGrid.Rows)
            {
                pointNames.Add((item as GridRow).Cells[0].Value.ToString());
            }
            if (hasTray)
            {
                foreach (var item in sGC_Tray.PrimaryGrid.Rows)
                {
                    Tray tray = ((item as GridRow).Tag as Tray);
                    //for (int i = 0; i < tray.RowCount * tray.ColCount; i++)
                    //{
                    //    pointNames.Add($"{tray.Name}-{i}");
                    //}
                    pointNames.Add($"{tray.Name}");
                }
            }
            return pointNames;
        }

        double lastXPos = 0;
        double lastYPos = 0;
        double lastZPos = 0;
        double lastRPos = 0;
        double lastHPos = 0;
        private void t_IO_Tick(object sender, EventArgs e)
        {
#if DEBUGWITHOUTMACHINE
#else
            if (!string.IsNullOrEmpty(XYNames))
            {
                xy_Displayer.Table.XPosition = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[0]].Position;
                xy_Displayer.Table.YPosition = SingletonProvider<MachineResource>.Instance.MachineAxis[XYNames.Split(',')[1]].Position;

                if (Math.Abs(lastXPos - xy_Displayer.Table.XPosition) > 0.02 ||
                    Math.Abs(lastYPos - xy_Displayer.Table.YPosition) > 0.02)
                {
                    xy_Displayer.UpdateBoard();
                    xy_Displayer.Repaint();
                }
                lastXPos = xy_Displayer.Table.XPosition;
                lastYPos = xy_Displayer.Table.YPosition;
                //
            }
            if (ZNames != null && ZNames.Count > 0)
            {
                SuperTabItem item = sTC_Z.SelectedTab;
                (item.AttachedControl.Controls[0] as MotionBoard).Table.YPosition = SingletonProvider<MachineResource>.Instance.MachineAxis[item.Name].Position;
                if (Math.Abs(lastZPos - (item.AttachedControl.Controls[0] as MotionBoard).Table.YPosition) > 0.02)
                {
                    (item.AttachedControl.Controls[0] as MotionBoard).UpdateBoard();
                    (item.AttachedControl.Controls[0] as MotionBoard).Repaint();
                }
                lastZPos = (item.AttachedControl.Controls[0] as MotionBoard).Table.YPosition;
            }
            if (HNames != null && HNames.Count > 0)
            {
                SuperTabItem item = sTC_H.SelectedTab;
                (item.AttachedControl.Controls[0] as MotionBoard).Table.XPosition = SingletonProvider<MachineResource>.Instance.MachineAxis[item.Name].Position;
                if (Math.Abs(lastHPos - (item.AttachedControl.Controls[0] as MotionBoard).Table.XPosition) > 0.02)
                {
                    (item.AttachedControl.Controls[0] as MotionBoard).UpdateBoard();
                    (item.AttachedControl.Controls[0] as MotionBoard).Repaint();
                }
                lastHPos = (item.AttachedControl.Controls[0] as MotionBoard).Table.XPosition;
            }
            if (RNames != null && RNames.Count > 0)
            {
                SuperTabItem item = sTC_R.SelectedTab;
                (item.AttachedControl.Controls[0] as MotionBoard).Table.RPosition = SingletonProvider<MachineResource>.Instance.MachineAxis[item.Name].Position;
                if (Math.Abs(lastRPos - (item.AttachedControl.Controls[0] as MotionBoard).Table.RPosition) > 0.02)
                {
                    (item.AttachedControl.Controls[0] as MotionBoard).UpdateBoard();
                    (item.AttachedControl.Controls[0] as MotionBoard).Repaint();
                }
                lastRPos = (item.AttachedControl.Controls[0] as MotionBoard).Table.RPosition;
            }
            foreach (var item in sGC_Input.PrimaryGrid.Rows)
            {
                if (item.Tag is Sensor)
                {
                    (item as GridRow).Cells[2].Value = (item.Tag as Sensor).WaitOn() ? "圆形选中32.png" : "圆形未选中32.png";
                }
            }
            foreach (var item in sGC_Output.PrimaryGrid.Rows)
            {
                if (item.Tag is Cylinder)
                {
                    if ((item.Tag as Cylinder).Origins != null && (item.Tag as Cylinder).Origins.Count > 0)
                        (item as GridRow).Cells[2].Value = (item.Tag as Cylinder).InOrigin() ? "圆形选中32.png" : "圆形未选中32.png";
                    else
                        (item as GridRow).Cells[2].Value = "圆形未选中32.png";
                    if ((item.Tag as Cylinder).Moves != null && (item.Tag as Cylinder).Moves.Count > 0)
                        (item as GridRow).Cells[3].Value = (item.Tag as Cylinder).InMove() ? "圆形选中32.png" : "圆形未选中32.png";
                    else
                        (item as GridRow).Cells[3].Value = "圆形未选中32.png";
                    (item as GridRow).Cells[1].Value = (item.Tag as Cylinder).AlreadyOn();
                }
            }
#endif
        }

        private void sGC_IO_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            if (e.GridCell.ColumnIndex == 1)
            {
                if (Convert.ToBoolean(e.GridCell.Value))
                    ((e.GridCell.GridRow).Tag as Cylinder).WaitOn();
                else
                    ((e.GridCell.GridRow).Tag as Cylinder).WaitOff();
            }
        }

        private void tSDDB_Show_DropDownOpening(object sender, EventArgs e)
        {
            tSDDB_Show.DropDownItems.Clear();

            if (!string.IsNullOrEmpty(XYNames))
            {
                addLayoutItem(XYNames.Split(',')[0], AxisType.H, false);
                addLayoutItem(XYNames.Split(',')[1], AxisType.V, false);
            }

            if (ZNames != null && ZNames.Count > 0)
            {
                foreach (var item in ZNames)
                {
                    addLayoutItem(item, AxisType.V, true);
                }
            }

            if (RNames != null && RNames.Count > 0)
            {
                foreach (var item in RNames)
                {
                    addLayoutItem(item, AxisType.R, true);
                }
            }

            if (HNames != null && HNames.Count > 0)
            {
                foreach (var item in HNames)
                {
                    addLayoutItem(item, AxisType.H, true);
                }
            }
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            Console.WriteLine("2222");
        }
        private async void GoToBoardPoint(MotionBoard board, string pointName, double xpos, double ypos, double rpos)
        {
            string stopReason1 = "";
            string stopReason2 = "";
            if (!string.IsNullOrEmpty(pointName))
            {
                if (board.Description.Equals(XYNames))
                {
                    if (MessageBox.Show($"确定移动到{pointName}点位吗?", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
#if DEBUGWITHOUTMACHINE
                        xy_Displayer.Table.XPosition = xpos;
                        xy_Displayer.Table.YPosition = ypos;
                        xy_Displayer.UpdateBoard();
#else

                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].Move2Point(pointName, out stopReason1, 0, false);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].Move2Point(pointName, out stopReason2, 0, false);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].Wait2Point(pointName, 0, out stopReason1);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].Wait2Point(pointName, 0, out stopReason2);

                            if (!string.IsNullOrEmpty(stopReason1) ||
                               !string.IsNullOrEmpty(stopReason2))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1},{stopReason2}", true);
                                }));
                            }
                        });
#endif
                    }
                }
                else if (HNames != null && HNames.Contains(board.Description))
                {
                    if (MessageBox.Show($"确定移动到{pointName}点位吗?", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[board.Description].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[board.Description].Move2Point(pointName, out stopReason1, 0);

                            if (!string.IsNullOrEmpty(stopReason1))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                }));
                            }
                        });
                    }
                }
                else if (ZNames != null && ZNames.Contains(board.Description))
                {
                    if (MessageBox.Show($"确定移动到{pointName}点位吗?", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[board.Description].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[board.Description].Move2Point(pointName, out stopReason1, 0);

                            if (!string.IsNullOrEmpty(stopReason1))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                }));
                            }
                        });
                    }
                }
                else if (RNames != null && RNames.Contains(board.Description))
                {
                    if (MessageBox.Show($"确定移动到{pointName}点位吗?", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[board.Description].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[board.Description].Move2Point(pointName, out stopReason1, 0);

                            if (!string.IsNullOrEmpty(stopReason1))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                }));
                            }
                        });
                    }
                }
            }
            else
            {
                if (board.Description.Equals(XYNames))
                {
                    if (MessageBox.Show($"确定移动到以下点位吗?\r\nX:{xpos}\r\nY:{ypos}", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
#if DEBUGWITHOUTMACHINE
                        xy_Displayer.Table.XPosition = xpos;
                        xy_Displayer.Table.YPosition = ypos;
                        xy_Displayer.UpdateBoard();
#else

                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].MoveAbsolute(xpos, out stopReason1, 0, false);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].MoveAbsolute(ypos, out stopReason2, 0, false);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].WaitAxisInPosition(xpos, 0, out stopReason1);
                            MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].WaitAxisInPosition(ypos, 0, out stopReason2);

                            if (!string.IsNullOrEmpty(stopReason1) ||
                               !string.IsNullOrEmpty(stopReason2))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1},{stopReason2}", true);
                                }));
                            }
                        });
#endif
                    }
                }
                else if (HNames != null && HNames.Contains(board.Description))
                {
                    if (MessageBox.Show($"确定移动到以下点位吗?\r\nH:{xpos}", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[board.Description].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[board.Description].MoveAbsolute(xpos, out stopReason1, 0);

                            if (!string.IsNullOrEmpty(stopReason1))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                }));
                            }
                        });
                    }
                }
                else if (ZNames != null && ZNames.Contains(board.Description))
                {
                    if (MessageBox.Show($"确定移动到以下点位吗?\r\nZ:{ypos}", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[board.Description].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[board.Description].MoveAbsolute(ypos, out stopReason1, 0);

                            if (!string.IsNullOrEmpty(stopReason1))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                }));
                            }
                        });
                    }
                }
                else if (RNames != null && RNames.Contains(board.Description))
                {
                    if (MessageBox.Show($"确定移动到以下点位吗?\r\nR:{rpos}", "移动至点位", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        await Task.Run(() =>
                        {
                            MachineResource.Instance.MachineAxis[board.Description].SetSpeed(SpeedType.Manual);
                            MachineResource.Instance.MachineAxis[board.Description].MoveAbsolute(rpos, out stopReason1, 0);

                            if (!string.IsNullOrEmpty(stopReason1))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                }));
                            }
                        });
                    }
                }
            }
        }
        private void KeyboardEnabled(MotionBoard board)
        {
            if (board.Description.Equals(XYNames))
            {
                FormKeyboard keyboardForm = new FormKeyboard();
                keyboardForm.OperateAxis1 = MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]];
                keyboardForm.OperateAxis2 = MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]];
                keyboardForm.Location = _mainForm.Location;
                keyboardForm.Size = _mainForm.Size;
                keyboardForm.ShowDialog();
            }
            else if (HNames != null && HNames.Contains(board.Description))
            {
                FormKeyboard keyboardForm = new FormKeyboard();
                keyboardForm.OperateAxis2 = null;
                keyboardForm.OperateAxis1 = MachineResource.Instance.MachineAxis[board.Description];
                keyboardForm.Location = _mainForm.Location;
                keyboardForm.Size = _mainForm.Size;
                keyboardForm.ShowDialog();
            }
            else if (ZNames != null && ZNames.Contains(board.Description))
            {
                FormKeyboard keyboardForm = new FormKeyboard();
                keyboardForm.OperateAxis1 = null;
                keyboardForm.OperateAxis2 = MachineResource.Instance.MachineAxis[board.Description];
                keyboardForm.Location = _mainForm.Location;
                keyboardForm.Size = _mainForm.Size;
                keyboardForm.ShowDialog();
            }
            //else if (RNames != null && RNames.Contains(board.Description))
            //{
            //    FormKeyboard keyboardForm = new FormKeyboard();
            //    keyboardForm.OperateAxis1 = MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]];
            //    keyboardForm.OperateAxis2 = MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]];
            //    keyboardForm.Location = _mainForm.Location;
            //    keyboardForm.Size = _mainForm.Size;
            //    keyboardForm.ShowDialog();
            //}
        }
        private void sGC_Point_PostRenderCell(object sender, GridPostRenderCellEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("路径"))
            {
                RenderButtonBackground2(e);
            }
            else if (e.GridCell.GridColumn.Name.Equals("到达"))
            {
                RenderButtonBackground(e);
            }
            else if (e.GridCell.GridColumn.Name.Contains("仅移动"))
            {
                RenderButtonBackground3(e);
            }
        }
        private void RenderButtonBackground2(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("设定路径"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.CornflowerBlue, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void RenderButtonBackground3(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("移动"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.FromArgb(192, 255, 192), 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void RenderButtonBackground(GridPostRenderCellEventArgs e)
        {
            if ((e.RenderParts & RenderParts.Background) == RenderParts.Background)
            {
                // Render a Red gradient background for
                // the slider if the cell Value is > 75

                if (e.GridCell.Value.ToString().Equals("运动至此"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.DeepSkyBlue, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (e.GridCell.Value.ToString().Equals("移动中,点击可停止"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.PaleVioletRed, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (e.GridCell.Value.ToString().Equals("Homing..."))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.Yellow, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else if (e.GridCell.Value.ToString().Equals("TimeOut"))
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.OrangeRed, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
                else
                {
                    Rectangle r = e.Bounds;

                    using (Brush br = new
                        LinearGradientBrush(r, Color.Transparent, Color.LimeGreen, 90f))
                    {
                        r.X++;
                        r.Width--;

                        e.Graphics.FillRectangle(br, r);
                    }
                }
            }
        }
        private void sGC_Point_CellMouseDown(object sender, GridCellMouseEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("设定路径"))
            {
                e.GridCell.CellStyles.Default.TextColor = Color.White;
            }
            else if (e.GridCell.GridColumn.Name.Equals("移动"))
            {
                e.GridCell.CellStyles.Default.TextColor = Color.White;
            }
        }

        private void sGC_Point_CellMouseUp(object sender, GridCellMouseEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("设定路径"))
            {
                e.GridCell.CellStyles.Default.TextColor = Color.Black;
            }
            else if (e.GridCell.GridColumn.Name.Equals("移动"))
            {
                e.GridCell.CellStyles.Default.TextColor = Color.Black;
            }
        }

        private async void sGC_Point_CellClick(object sender, GridCellClickEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("路径"))
            {
                string name = e.GridCell.GridRow.Cells[0].Value.ToString();
                FormPath pathForm = null;
                if (SingletonProvider<MachineResource>.Instance.ModulePaths.Count > 0)
                {
                    if (!MachineResource.Instance.ModulePaths.ContainsKey(_moduleName))
                        MachineResource.Instance.ModulePaths.Add(_moduleName, new SerializableDictionary<string, PointPath>());
                    if (!SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(name))
                        SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].Add(name, new PointPath());
                    pathForm = new FormPath(getAllPointsName(), SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][name]);
                    pathForm.StartPosition = FormStartPosition.CenterScreen;
                    if (pathForm.ShowDialog() == DialogResult.OK)
                    {
                        MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                    }
                }
                else
                {
                    pathForm = new FormPath(getAllPointsName(), null);
                    pathForm.StartPosition = FormStartPosition.CenterScreen;
                    if (pathForm.ShowDialog() == DialogResult.OK)
                    {
                        if (!SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
                            SingletonProvider<MachineResource>.Instance.ModulePaths.Add(_moduleName, new SerializableDictionary<string, PointPath>());
                        SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].Add(name, pathForm.CurrentPointPath);
                        MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                    }
                }
            }
            else if (e.GridCell.Value.Equals("运动至此"))
            {
                string pointName = e.GridCell.GridRow.Tag as string;
                if (SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
                {
                    if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(pointName))
                    {
                        e.GridCell.Value = "移动中,点击可停止";
                        PointPath pp = SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][pointName];
                        string errInfo = "";
                        await Task.Run(() =>
                        {
                            pp.Prepare(MachineResource.Instance.MachineAxis, MachineResource.Instance.MachineCylinder);
                            pp.GoTo(MachineResource.Instance.MachineAxis, MachineResource.Instance.MachineCylinder, MachineResource.Instance.MachineSensor, out errInfo);
                            if (!string.IsNullOrEmpty(errInfo))
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, errInfo, true);
                                    e.GridCell.Value = "运动至此";
                                }));
                            }
                            else
                            {
                                this.BeginInvoke(new Action(() =>
                                {
                                    SystemInfo.ShowToastInfo(this, "移动完成", false);
                                    e.GridCell.Value = "运动至此";
                                }));
                            }
                        });
                    }
                    else
                    {
                        this.BeginInvoke(new Action(() =>
                        {
                            SystemInfo.ShowToastInfo(this, "尚未编辑移动路径", true);
                            e.GridCell.Value = "运动至此";
                        }));
                    }
                }
                else
                {
                    SystemInfo.ShowToastInfo(this, "尚未编辑移动路径!", true);
                }
            }
            else if (e.GridCell.Value.Equals("移动中,点击可停止") && e.GridCell.GridColumn.Name.Equals("到达"))
            {
                string pointName = e.GridCell.GridRow.Tag as string;
                if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(pointName))
                {
                    PointPath pp = SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][pointName];
                    pp.Stop(SingletonProvider<MachineResource>.Instance.MachineAxis);
                    this.BeginInvoke(new Action(() =>
                    {
                        e.GridCell.Value = "运动至此";
                    }));
                }
            }
            else if (e.GridCell.Value.Equals("移动") && e.GridCell.GridColumn.Name.Contains("仅移动"))
            {
                e.GridCell.Value = "移动中,点击可停止";
                string pointName = e.GridCell.GridRow.Tag as string;
                string axisName = e.GridCell.GridColumn.Name.Substring(3);
                //if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(pointName))
                //{
                string stopReason1 = "";
                string stopReason2 = "";
                await Task.Run(() =>
                {
                        //if(axisName.Equals("XY"))
                        //{
                        //    MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].SetSpeed(SpeedType.Manual);
                        //    MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].SetSpeed(SpeedType.Manual);
                        //    MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].Move2Point(pointName, out stopReason1, 0, false);
                        //    MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].Move2Point(pointName, out stopReason2, 0, false);
                        //    MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].Wait2Point(pointName, 0, out stopReason1);
                        //    MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].Wait2Point(pointName, 0, out stopReason2);

                        //    if (!string.IsNullOrEmpty(stopReason1) ||
                        //        !string.IsNullOrEmpty(stopReason2))
                        //    {
                        //        this.BeginInvoke(new Action(() =>
                        //        {
                        //            SystemInfo.ShowToastInfo(this, $"{stopReason1},{stopReason2}", true);
                        //            e.GridCell.Value = "移动";
                        //        }));
                        //    }
                        //    else
                        //    {
                        //        this.BeginInvoke(new Action(() =>
                        //        {
                        //            SystemInfo.ShowToastInfo(this, $"移动完成", false);
                        //            e.GridCell.Value = "移动";
                        //        }));
                        //    }
                        //}
                        //else
                        //{
                        MachineResource.Instance.MachineAxis[axisName].SetSpeed(SpeedType.Manual);
                    MachineResource.Instance.MachineAxis[axisName].Move2Point(pointName, out stopReason1, 0);

                    if (!string.IsNullOrEmpty(stopReason1))
                    {
                        this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, $"{stopReason1}", true);
                                e.GridCell.Value = "移动";
                            }));
                    }
                    else
                    {
                        this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, $"移动完成", false);
                                e.GridCell.Value = "移动";
                            }));
                    }
                        //}
                    });
                //}
            }
            else if (e.GridCell.GridColumn.Name.Contains("仅移动") && e.GridCell.Value.Equals("移动中,点击可停止"))
            {
                string pointName = e.GridCell.GridRow.Tag as string;
                string axisName = e.GridCell.GridColumn.Name.Substring(3);
                //if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(pointName))
                //{
                if (axisName.Equals("XY"))
                {
                    MachineResource.Instance.MachineAxis[XYNames.Split(',')[0]].Stop(false);
                    MachineResource.Instance.MachineAxis[XYNames.Split(',')[1]].Stop(false);
                }
                else
                {
                    MachineResource.Instance.MachineAxis[axisName].Stop(false);
                }
                this.BeginInvoke(new Action(() =>
                {
                    e.GridCell.Value = "移动";
                }));
                //}
            }
        }

        private void 编辑矩阵点位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sGC_Tray.GetSelectedRows().Count == 0)
                return;
            Tray tray = sGC_Tray.GetSelectedRows()[0].Tag as Tray;
            if (tray != null)
            {
                string zName = ZNames == null || ZNames.Count == 0 ? "" : ZNames[0];
                string rName = RNames == null || RNames.Count == 0 ? "" : RNames[0];
                FormTray trayForm = new FormTray(tray.XAxisName + "," + tray.YAxisName, zName, rName, _moduleName, tray);
                trayForm.IsLoad = true;
                if (trayForm.ShowDialog() == DialogResult.OK)
                {
                    editTrayRow(sGC_Tray.GetSelectedRows()[0] as GridRow, tray);
                    //if(trayForm.CornerChanged)
                    {
                        trayForm.EditTray.Helper.Calibrate();
                        bool addZR = true;
                        for (int i = 0; i < trayForm.EditTray.RowCount * trayForm.EditTray.ColCount; i++)
                        {
                            PointF p = trayForm.EditTray.Helper.GetPoint(i);
                            if (i > 0)
                                addZR = false;
                            refreshPointOnBoard($"{trayForm.EditTray.Name}_{i}", p.X, p.Y, new List<double> { trayForm.EditTray.NozzleZ }, new List<double>() { trayForm.EditTray.NozzleR }, null, "", addZR);
                        }
                    }
                }
            }
        }

        private void sGC_Tray_PostRenderCell(object sender, GridPostRenderCellEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("路径"))
            {
                RenderButtonBackground2(e);
            }
            else if (e.GridCell.GridColumn.Name.Equals("到达"))
            {
                RenderButtonBackground(e);
            }
            //else if (e.GridCell.GridColumn.Name.Contains("仅移动"))
            //{
            //    RenderButtonBackground3(e);
            //}
        }

        private void sGC_Tray_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            Tray tray = e.GridCell.GridRow.Tag as Tray;
            if (e.GridCell.GridColumn.Name.Equals("名称"))
            {
                string oldName = tray.Name;
                tray.Name = e.GridCell.Value.ToString();
                MachineResource.Instance.ModuleTrays[_moduleName].Remove(oldName);
                MachineResource.Instance.ModuleTrays[_moduleName].Add(tray.Name, tray);
                if (MachineResource.Instance.ModulePaths.ContainsKey(_moduleName) && MachineResource.Instance.ModulePaths[_moduleName].ContainsKey(oldName))
                {
                    PointPath pp = MachineResource.Instance.ModulePaths[_moduleName][oldName];
                    MachineResource.Instance.ModulePaths[_moduleName].Remove(oldName);
                    MachineResource.Instance.ModulePaths[_moduleName].Add(tray.Name, pp);
                }
                updateTray(tray, oldName);
            }
            else if (e.GridCell.GridColumn.Name.Equals("行数"))
            {
                tray.RowCount = Convert.ToInt32(e.GridCell.Value);
                updateTray(tray);
            }
            else if (e.GridCell.GridColumn.Name.Equals("列数"))
            {
                tray.ColCount = Convert.ToInt32(e.GridCell.Value);
                updateTray(tray);
            }
            else if (e.GridCell.GridColumn.Name.Equals("吸嘴数"))
            {
                tray.NozzleCount = Convert.ToInt32(e.GridCell.Value);
                updateTray(tray);
            }
            else if (e.GridCell.GridColumn.Name.Equals("吸嘴间距X"))
            {
                tray.OffsetX = Convert.ToDouble(e.GridCell.Value);
                updateTray(tray);
            }
            else if (e.GridCell.GridColumn.Name.Equals("吸嘴间距Y"))
            {
                tray.OffsetY = Convert.ToDouble(e.GridCell.Value);
                updateTray(tray);
            }
            else if (e.GridCell.GridColumn.Name.Equals("Z轴高度"))
            {
                tray.NozzleZ = Convert.ToDouble(e.GridCell.Value);
                updateTray(tray);
            }
            else if (e.GridCell.GridColumn.Name.Equals("R轴角度"))
            {
                tray.NozzleR = Convert.ToDouble(e.GridCell.Value);
                updateTray(tray);
            }
        }
        private void updateTray(Tray tray, string oldName = "")
        {
            foreach (Axis axis in MachineResource.Instance.MachineAxis.Values)
            {
                string curAxisName = axis.Description;
                if ((!curAxisName.Equals(tray.RAxisName)) &&
                    !curAxisName.Equals(tray.XAxisName) &&
                    !curAxisName.Equals(tray.ZAxisName) &&
                    !curAxisName.Equals(tray.YAxisName))
                    continue;
                List<string> removeNames = new List<string>();
                foreach (var p in axis.MotionPosition)
                {
                    if (!string.IsNullOrEmpty(oldName))
                    {
                        if (p.Key.Contains(oldName))
                        {
                            //axis.MotionPosition.Remove(names[i]);
                            removeNames.Add(p.Key);
                            //break;
                        }
                    }
                    else
                    {
                        if (p.Key.Contains(tray.Name))
                        {
                            //axis.MotionPosition.Remove(names[i]);
                            removeNames.Add(p.Key);
                            //break;
                        }
                    }
                }

                removeNames.ForEach(name => axis.MotionPosition.Remove(name));

                tray.Helper.Calibrate();
                for (int i = 0; i < tray.RowCount * tray.ColCount; i++)
                {
                    for (int j = 0; j < tray.NozzleCount; j++)
                    {
                        if (axis.MotionPosition.ContainsKey($"{tray.Name}-{i}-{j}"))
                        {
                            if (axis.Description.Equals(tray.XAxisName))
                            {
                                axis.MotionPosition[$"{tray.Name}-{i}-{j}"] = tray.Helper.GetPoint(i).X + tray.OffsetX * j;
                            }
                            else if (axis.Description.Equals(tray.YAxisName))
                            {
                                axis.MotionPosition[$"{tray.Name}-{i}-{j}"] = tray.Helper.GetPoint(i).Y + tray.OffsetY * j;
                            }
                            else if (axis.Description.Equals(tray.ZAxisName))
                            {
                                axis.MotionPosition[$"{tray.Name}-{i}-{j}"] = tray.Helper.GetZ();
                            }
                            else if (axis.Description.Equals(tray.RAxisName))
                            {
                                axis.MotionPosition[$"{tray.Name}-{i}-{j}"] = tray.Helper.GetR();
                            }
                        }
                        else
                        {
                            if (axis.Description.Equals(tray.XAxisName))
                            {
                                axis.MotionPosition.Add($"{tray.Name}-{i}-{j}", tray.Helper.GetPoint(i).X + tray.OffsetX * j);
                            }
                            else if (axis.Description.Equals(tray.YAxisName))
                            {
                                axis.MotionPosition.Add($"{tray.Name}-{i}-{j}", tray.Helper.GetPoint(i).Y + tray.OffsetY * j);
                            }
                            else if (axis.Description.Equals(tray.ZAxisName))
                            {
                                axis.MotionPosition.Add($"{tray.Name}-{i}-{j}", tray.Helper.GetZ());
                            }
                            else if (axis.Description.Equals(tray.RAxisName))
                            {
                                axis.MotionPosition.Add($"{tray.Name}-{i}-{j}", tray.Helper.GetR());
                            }
                        }
                    }
                }

                for (int i = 0; i < tray.RowCount * tray.ColCount; i++)
                {
                    PointF p = tray.Helper.GetPoint(i);
                    refreshPointOnBoard($"{tray.Name}_{i}", p.X, p.Y, new List<double>() { tray.NozzleZ }, new List<double>() { tray.NozzleR }, null);
                }
            }
        }
        private void sGC_Tray_CellMouseDown(object sender, GridCellMouseEventArgs e)
        {

        }

        private void sGC_Tray_CellMouseUp(object sender, GridCellMouseEventArgs e)
        {

        }

        private void sGC_Tray_CellClick(object sender, GridCellClickEventArgs e)
        {
            if (e.GridCell.GridColumn.Name.Equals("路径"))
            {
                string name = e.GridCell.GridRow.Cells[0].Value.ToString();
                FormPath pathForm = null;
                if (SingletonProvider<MachineResource>.Instance.ModulePaths.Count > 0 && SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
                {
                    if (!SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(name))
                        SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].Add(name, new PointPath());
                    pathForm = new FormPath(getAllPointsName(true), SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][name]);
                    pathForm.StartPosition = FormStartPosition.CenterScreen;
                    if (pathForm.ShowDialog() == DialogResult.OK)
                    {
                        MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                    }
                }
                else
                {
                    pathForm = new FormPath(getAllPointsName(true), null);
                    pathForm.StartPosition = FormStartPosition.CenterScreen;
                    if (pathForm.ShowDialog() == DialogResult.OK)
                    {
                        if (!SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
                            SingletonProvider<MachineResource>.Instance.ModulePaths.Add(_moduleName, new SerializableDictionary<string, PointPath>());
                        SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].Add(name, pathForm.CurrentPointPath);
                        MachineResource.Instance.Save(Properties.Settings.Default.ConfigName);
                    }
                }
            }
            else if (e.GridCell.GridColumn.Name.Equals("到达"))
            {
                FormMoveTray moveTrayForm = new FormMoveTray(e.GridCell.GridRow.Tag as Tray, _moduleName);
                moveTrayForm.StartPosition = FormStartPosition.CenterParent;
                moveTrayForm.ShowDialog();
            }
        }
        public void LoadLayout()
        {
            if (this.ContainerSplitDistance != null && this.ContainerSplitDistance.Count > 0)
            {
                this.sC_LeftRight.SplitterDistance = this.ContainerSplitDistance[0];
                this.sC_LeftRightDown.SplitterDistance = this.ContainerSplitDistance[1];
                this.sC_UpDownOuter.SplitterDistance = this.ContainerSplitDistance[2];
            }
            else
            {
                this.sC_LeftRight.SplitterDistance = this.sC_LeftRight.Width * 7 / 10;
                this.sC_LeftRightDown.SplitterDistance = this.sC_LeftRightDown.Width * 1 / 2;
                this.sC_UpDownOuter.SplitterDistance = this.sC_UpDownOuter.Height * 7 / 10;
            }
        }
        public void SaveLayout()
        {
            this.ContainerSplitDistance.Clear();
            this.ContainerSplitDistance.Add(sC_LeftRight.SplitterDistance);
            this.ContainerSplitDistance.Add(sC_LeftRightDown.SplitterDistance);
            this.ContainerSplitDistance.Add(sC_UpDownOuter.SplitterDistance);
        }

        private void 删除矩阵点位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除该矩阵吗?", "删除矩阵", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                if (sGC_Tray.GetSelectedRows().Count == 0)
                {
                    SystemInfo.ShowToastInfo(this, "请先选中需要删除的矩阵点位", true);
                    return;
                }
                Tray tray = sGC_Tray.GetSelectedRows()[0].Tag as Tray;
                foreach (Axis axis in MachineResource.Instance.MachineAxis.Values)
                {
                    string curAxisName = axis.Description;
                    if ((!curAxisName.Equals(tray.RAxisName)) &&
                        !curAxisName.Equals(tray.XAxisName) &&
                        !curAxisName.Equals(tray.ZAxisName) &&
                        !curAxisName.Equals(tray.YAxisName))
                        continue;
                    List<string> allRemoveNames = new List<string>();
                    foreach (var p in axis.MotionPosition)
                    {
                        if (p.Key.Contains(tray.Name))
                        {
                            allRemoveNames.Add(p.Key);
                        }
                    }
                    allRemoveNames.ForEach(name => axis.MotionPosition.Remove(name));
                }
                MachineResource.Instance.ModuleTrays[_moduleName].Remove(tray.Name);
                sGC_Tray.PrimaryGrid.Rows.Remove(sGC_Tray.GetSelectedRows()[0]);
            }
        }

        private void toolStripMenuItem4_DropDownOpening(object sender, EventArgs e)
        {
            if (_editTray != null)
            {
                if (_editTray.RowCount == 1 || _editTray.ColCount == 1)
                {
                    右上ToolStripMenuItem.Enabled = false;
                    左下ToolStripMenuItem.Enabled = false;
                }
                else
                {
                    右上ToolStripMenuItem.Enabled = true;
                    左下ToolStripMenuItem.Enabled = true;
                }
            }
        }

        private void cMS_Point_Opening(object sender, CancelEventArgs e)
        {
            if (sGC_Point.GetSelectedRows().Count > 0)
            {
                添加自定义点位ToolStripMenuItem.Enabled = true;
                删除点位ToolStripMenuItem.Enabled = true;
            }
            else
            {
                添加自定义点位ToolStripMenuItem.Enabled = false;
                删除点位ToolStripMenuItem.Enabled = false;
            }
        }

        private void sGC_Output_CellValueChanged(object sender, GridCellValueChangedEventArgs e)
        {
            Cylinder cylinderOld = e.GridCell.GridRow.Tag as Cylinder;

            if (e.GridCell.GridColumn.Name.Equals("操作"))
            {
                if (Convert.ToBoolean(e.GridCell.Value))
                    cylinderOld.On();
                else
                    cylinderOld.Off();
            }
        }

        private void FormXYZR_Activated(object sender, EventArgs e)
        {

        }

        private void FormXYZR_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                tSB_Mode.Checked = true;
                if (tSB_Mode.Checked)
                {
                    tSB_Mode.Text = "寸动";
                    tSB_Mode.Image = Properties.Resources.寸动;
                    ContiMove = false;
                    //tSB_Mode.BackColor = Color.YellowGreen;
                    tSDDB_Step.Enabled = true;
                    tSL_Step.Enabled = true;
                }
                else
                {
                    tSB_Mode.Text = "连续";
                    tSB_Mode.Image = Properties.Resources.连续;
                    ContiMove = true;
                    //tSB_Mode.BackColor = Color.BlueViolet;
                    tSDDB_Step.Enabled = false;
                    tSL_Step.Enabled = false;
                }
            }
        }
    }

    public enum AxisType
    {
        H,
        V,
        R,
    }
}
